| 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 | 486 | void Stopwatch::initialize() { | |
| 50 | 486 | process_start_time_ = now(); | |
| 51 | 486 | global_stats_ = | |
| 52 |
5/8✓ Branch 1 taken 486 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 486 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 237 times.
✓ Branch 7 taken 249 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 237 times.
|
723 | CmdLine::arg_is_declared("sys:stats") && |
| 53 |
3/8✓ Branch 1 taken 237 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 237 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 486 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
723 | CmdLine::get_arg_bool("sys:stats") ; |
| 54 | |||
| 55 | 486 | } | |
| 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 | 1401 | Stopwatch::Stopwatch(const std::string& task_name, bool verbose) : | |
| 65 | 1401 | start_(std::chrono::system_clock::now()), | |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1401 times.
|
1401 | task_name_(task_name), |
| 67 | 1401 | verbose_(verbose) | |
| 68 | { | ||
| 69 |
2/2✓ Branch 0 taken 640 times.
✓ Branch 1 taken 761 times.
|
1401 | if(verbose_) { |
| 70 |
1/2✓ Branch 1 taken 640 times.
✗ Branch 2 not taken.
|
640 | Logger::out(task_name_) << "Start..." << std::endl; |
| 71 |
2/2✓ Branch 0 taken 456 times.
✓ Branch 1 taken 184 times.
|
640 | if(task_name_ != "Total time") { |
| 72 |
1/2✓ Branch 1 taken 456 times.
✗ Branch 2 not taken.
|
456 | Logger::instance()->indent(); |
| 73 | } | ||
| 74 | } | ||
| 75 | 1401 | } | |
| 76 | |||
| 77 | 32 | Stopwatch::Stopwatch() : | |
| 78 | 32 | start_(std::chrono::system_clock::now()), | |
| 79 | 32 | verbose_(false) | |
| 80 | { | ||
| 81 | 32 | } | |
| 82 | |||
| 83 | 693 | 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 | 693 | 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 | 693 | return 0.001 * double(elapsed_milliseconds.count()); | |
| 92 | } | ||
| 93 | |||
| 94 | 1072 | double Stopwatch::now() { | |
| 95 | 1072 | 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 | 1072 | return 0.001 * double(elapsed_milliseconds.count()); | |
| 100 | } | ||
| 101 | |||
| 102 | 1433 | Stopwatch::~Stopwatch() { | |
| 103 |
2/2✓ Branch 0 taken 640 times.
✓ Branch 1 taken 793 times.
|
1433 | if(verbose_) { |
| 104 |
2/2✓ Branch 0 taken 456 times.
✓ Branch 1 taken 184 times.
|
640 | if(task_name_ != "Total time") { |
| 105 | 456 | Logger::instance()->unindent(); | |
| 106 | } | ||
| 107 | 640 | print_elapsed_time(); | |
| 108 | } | ||
| 109 | 1433 | } | |
| 110 | |||
| 111 | 640 | void Stopwatch::print_elapsed_time() { | |
| 112 | 640 | Logger::out(task_name_) | |
| 113 | 640 | << "Elapsed: " << String::format_time(elapsed_time()) | |
| 114 | << std::endl; | ||
| 115 | 640 | } | |
| 116 | |||
| 117 | } | ||
| 118 |