GCC Code Coverage Report


Directory: ./
File: lib/geogram/basic/stopwatch.cpp
Date: 2026-09-07 02:28:19
Exec Total Coverage
Lines: 35 38 92.1%
Functions: 7 8 87.5%
Branches: 19 32 59.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #include <geogram/basic/stopwatch.h>
41 #include <geogram/basic/command_line.h>
42 #include <iostream>
43
44 namespace GEO {
45
46 double Stopwatch::process_start_time_ = 0.0;
47 bool Stopwatch::global_stats_ = false;
48
49 490 void Stopwatch::initialize() {
50 490 process_start_time_ = now();
51 490 global_stats_ =
52
5/8
✓ Branch 1 taken 490 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 490 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 239 times.
✓ Branch 7 taken 251 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 239 times.
729 CmdLine::arg_is_declared("sys:stats") &&
53
3/8
✓ Branch 1 taken 239 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 239 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 490 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
729 CmdLine::get_arg_bool("sys:stats") ;
54
55 490 }
56
57 void Stopwatch::show_stats() {
58 Logger::out("Process") << "Total elapsed time: "
59 << process_elapsed_time()
60 << "s" << std::endl;
61 // TODO: specific stats.
62 }
63
64 1421 Stopwatch::Stopwatch(const std::string& task_name, bool verbose) :
65 1421 start_(std::chrono::system_clock::now()),
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1421 times.
1421 task_name_(task_name),
67 1421 verbose_(verbose)
68 {
69
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 761 times.
1421 if(verbose_) {
70
1/2
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
660 Logger::out(task_name_) << "Start..." << std::endl;
71
2/2
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 186 times.
660 if(task_name_ != "Total time") {
72
1/2
✓ Branch 1 taken 474 times.
✗ Branch 2 not taken.
474 Logger::instance()->indent();
73 }
74 }
75 1421 }
76
77 36 Stopwatch::Stopwatch() :
78 36 start_(std::chrono::system_clock::now()),
79 36 verbose_(false)
80 {
81 36 }
82
83 717 double Stopwatch::elapsed_time() const {
84 // OMG, such nonsense ...
85 // ... but well, lets me get time with reasonable resolution
86 // in a portable way.
87 717 auto now(std::chrono::system_clock::now());
88 auto elapsed = now-start_;
89 auto elapsed_milliseconds =
90 std::chrono::duration_cast<std::chrono::milliseconds>(elapsed);
91 717 return 0.001 * double(elapsed_milliseconds.count());
92 }
93
94 1098 double Stopwatch::now() {
95 1098 auto now(std::chrono::system_clock::now());
96 auto elapsed = now.time_since_epoch();
97 auto elapsed_milliseconds =
98 std::chrono::duration_cast<std::chrono::milliseconds>(elapsed);
99 1098 return 0.001 * double(elapsed_milliseconds.count());
100 }
101
102 1457 Stopwatch::~Stopwatch() {
103
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 797 times.
1457 if(verbose_) {
104
2/2
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 186 times.
660 if(task_name_ != "Total time") {
105 474 Logger::instance()->unindent();
106 }
107 660 print_elapsed_time();
108 }
109 1457 }
110
111 660 void Stopwatch::print_elapsed_time() {
112 660 Logger::out(task_name_)
113 660 << "Elapsed: " << String::format_time(elapsed_time())
114 << std::endl;
115 660 }
116
117 }
118