77// Licensed under the Apache License, Version 2.0 (the "License");
88// you may not use this file except in compliance with the License.
99// You may obtain a copy of the License at
10- //
10+ //
1111// http ://www.apache.org/licenses/LICENSE-2.0
12- //
12+ //
1313// Unless required by applicable law or agreed to in writing, software
1414// distributed under the License is distributed on an "AS IS" BASIS,
1515// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2323#define DNATIMER_H_
2424
2525#include < chrono>
26- #include < string>
27- #include < sstream>
2826#include < iomanip>
27+ #include < sstream>
28+ #include < string>
2929
3030namespace dynadjust {
3131
3232// High-precision timer class to replace boost::timer::cpu_timer
3333class cpu_timer {
34- public:
34+ public:
3535 struct cpu_times {
3636 std::chrono::nanoseconds wall;
3737 std::chrono::nanoseconds user;
3838 std::chrono::nanoseconds system;
3939 };
4040
4141 cpu_timer () { start (); }
42-
43- void start () {
44- start_time_ = std::chrono::high_resolution_clock::now ();
45- }
46-
47- void resume () {
48- start ();
49- }
50-
42+
43+ void start () { start_time_ = std::chrono::high_resolution_clock::now (); }
44+
45+ void resume () { start (); }
46+
5147 void stop () {
5248 // For compatibility, but no-op since we calculate elapsed on demand
5349 }
54-
50+
5551 cpu_times elapsed () const {
5652 auto end_time = std::chrono::high_resolution_clock::now ();
5753 auto wall_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time_);
58- return {wall_duration, wall_duration, wall_duration}; // For simplicity, user and system = wall
54+ return {wall_duration, wall_duration, wall_duration}; // For simplicity, user and system = wall
5955 }
60-
56+
6157 std::string format (int places = 6 ) const {
6258 auto times = elapsed ();
6359 double wall_seconds = times.wall .count () / 1e9 ;
64-
60+
6561 std::ostringstream oss;
6662 oss << std::fixed << std::setprecision (places);
6763 oss << wall_seconds << " s wall" ;
6864 return oss.str ();
6965 }
7066
71- private:
67+ private:
7268 std::chrono::high_resolution_clock::time_point start_time_;
7369};
7470
75- inline std::string format_wall_time (std::chrono::nanoseconds wall) {
76- double seconds = wall.count () / 1.0e9 ;
71+ // Format an elapsed duration (in seconds) for display.
72+ // < 1 second -> "X.XXXms"
73+ // 1..60 second -> "X.XXXs"
74+ // >= 60 second -> "hh:mm:ss"
75+ inline std::string FormatElapsedTime (double seconds) {
7776 std::ostringstream oss;
78- if (seconds >= 1.0 )
79- oss << std::fixed << std::setprecision (3 ) << seconds << " s" ;
80- else
77+ if (seconds >= 60.0 ) {
78+ long total_seconds = static_cast <long >(seconds);
79+ long hours = total_seconds / 3600 ;
80+ long minutes = (total_seconds % 3600 ) / 60 ;
81+ long secs = total_seconds % 60 ;
82+ oss << std::setfill (' 0' ) << std::setw (2 ) << hours << ' :' << std::setw (2 ) << minutes << ' :' << std::setw (2 )
83+ << secs;
84+ } else if (seconds >= 1.0 ) {
85+ oss << std::fixed << std::setprecision (3 ) << seconds << ' s' ;
86+ } else {
8187 oss << std::fixed << std::setprecision (3 ) << (seconds * 1000.0 ) << " ms" ;
88+ }
8289 return oss.str ();
8390}
8491
8592} // namespace dynadjust
8693
87- #endif // DNATIMER_H_
94+ #endif // DNATIMER_H_
0 commit comments