@@ -29,7 +29,7 @@ namespace {
2929 using dataElementType = std::pair<std::string, TimerResultsData>;
3030 bool more_second_sec (const dataElementType& lhs, const dataElementType& rhs)
3131 {
32- return lhs.second .seconds () > rhs.second .seconds ();
32+ return lhs.second .getSeconds () > rhs.second .getSeconds ();
3333 }
3434
3535 // TODO: remove and print through (synchronized) ErrorLogger instead
@@ -38,12 +38,10 @@ namespace {
3838
3939// TODO: this does not include any file context when SHOWTIME_FILE thus rendering it useless - should we include the logging with the progress logging?
4040// that could also get rid of the broader locking
41- void TimerResults::showResults (SHOWTIME_MODES mode) const
41+ void TimerResults::showResults (ShowTime mode) const
4242{
43- if (mode == SHOWTIME_MODES::SHOWTIME_NONE || mode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL )
43+ if (mode == ShowTime::NONE || mode == ShowTime::FILE_TOTAL )
4444 return ;
45-
46- TimerResultsData overallData;
4745 std::vector<dataElementType> data;
4846
4947 {
@@ -61,38 +59,20 @@ void TimerResults::showResults(SHOWTIME_MODES mode) const
6159
6260 size_t ordinal = 1 ; // maybe it would be nice to have an ordinal in output later!
6361 for (auto iter=data.cbegin (); iter!=data.cend (); ++iter) {
64- const double sec = iter->second .seconds ();
62+ const double sec = iter->second .getSeconds (). count ();
6563 const double secAverage = sec / static_cast <double >(iter->second .mNumberOfResults );
66- bool hasParent = false ;
67- {
68- // Do not use valueFlow.. in "Overall time" because those are included in Tokenizer already
69- if (startsWith (iter->first ," valueFlow" ))
70- hasParent = true ;
71-
72- // Do not use inner timers in "Overall time"
73- const std::string::size_type pos = iter->first .rfind (" ::" );
74- if (pos != std::string::npos)
75- hasParent = std::any_of (data.cbegin (), data.cend (), [iter,pos](const dataElementType& d) {
76- return d.first .size () == pos && iter->first .compare (0 , d.first .size (), d.first ) == 0 ;
77- });
78- }
79- if (!hasParent)
80- overallData.mClocks += iter->second .mClocks ;
81- if ((mode != SHOWTIME_MODES::SHOWTIME_TOP5_FILE && mode != SHOWTIME_MODES::SHOWTIME_TOP5_SUMMARY) || (ordinal<=5 )) {
64+ if ((mode != ShowTime::TOP5_FILE && mode != ShowTime::TOP5_SUMMARY) || (ordinal<=5 )) {
8265 std::cout << iter->first << " : " << sec << " s (avg. " << secAverage << " s - " << iter->second .mNumberOfResults << " result(s))" << std::endl;
8366 }
8467 ++ordinal;
8568 }
86-
87- const double secOverall = overallData.seconds ();
88- std::cout << " Overall time: " << secOverall << " s" << std::endl;
8969}
9070
91- void TimerResults::addResults (const std::string& str, std::clock_t clocks )
71+ void TimerResults::addResults (const std::string& str, std::chrono::milliseconds duration )
9272{
9373 std::lock_guard<std::mutex> l (mResultsSync );
9474
95- mResults [str].mClocks += clocks ;
75+ mResults [str].mDuration += duration ;
9676 mResults [str].mNumberOfResults ++;
9777}
9878
@@ -102,17 +82,12 @@ void TimerResults::reset()
10282 mResults .clear ();
10383}
10484
105- Timer::Timer (std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults)
106- : mStr(std::move(str))
107- , mTimerResults(timerResults)
108- , mStart(std::clock())
109- , mShowTimeMode(showtimeMode)
110- , mStopped(showtimeMode == SHOWTIME_MODES::SHOWTIME_NONE || showtimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL)
111- {}
112-
113- Timer::Timer (bool fileTotal, std::string filename)
114- : mStr(std::move(filename))
115- , mStopped(!fileTotal)
85+ Timer::Timer (std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults, Type type)
86+ : mName(std::move(str))
87+ , mMode(showtimeMode)
88+ , mType(type)
89+ , mStart(Clock::now())
90+ , mResults(timerResults)
11691{}
11792
11893Timer::~Timer ()
@@ -122,23 +97,49 @@ Timer::~Timer()
12297
12398void Timer::stop ()
12499{
125- if ((mShowTimeMode != SHOWTIME_MODES::SHOWTIME_NONE) && !mStopped ) {
126- const std::clock_t end = std::clock ();
127- const std::clock_t diff = end - mStart ;
128-
129- if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE) {
130- const double sec = static_cast <double >(diff) / CLOCKS_PER_SEC;
131- std::lock_guard<std::mutex> l (stdCoutLock);
132- std::cout << mStr << " : " << sec << " s" << std::endl;
133- } else if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) {
134- const double sec = static_cast <double >(diff) / CLOCKS_PER_SEC;
100+ if (mMode == ShowTime::NONE)
101+ return ;
102+ if (mType == Type::OVERALL && mMode != ShowTime::TOP5_SUMMARY && mMode != ShowTime::SUMMARY) {
103+ mMode = ShowTime::NONE;
104+ return ;
105+ }
106+ if (mType == Type::FILE && mMode != ShowTime::TOP5_FILE && mMode != ShowTime::FILE && mMode != ShowTime::FILE_TOTAL) {
107+ mMode = ShowTime::NONE;
108+ return ;
109+ }
110+ if (mStart != TimePoint{}) {
111+ auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now () - mStart );
112+ if (!mResults ) {
135113 std::lock_guard<std::mutex> l (stdCoutLock);
136- std::cout << " Check time: " << mStr << " : " << sec << " s " << std::endl;
114+ std::cout << ( mType == Type::OVERALL ? " Overall time: " : " Check time: " + mName + " : " ) << TimerResultsData::durationToString (diff) << std::endl;
137115 } else {
138- if (mTimerResults )
139- mTimerResults ->addResults (mStr , diff);
116+ mResults ->addResults (mName , diff);
140117 }
141118 }
119+ mMode = ShowTime::NONE; // prevent multiple stops
120+ }
142121
143- mStopped = true ;
122+ std::string TimerResultsData::durationToString (std::chrono::milliseconds duration)
123+ {
124+ // Extract hours
125+ auto hours = std::chrono::duration_cast<std::chrono::hours>(duration);
126+ duration -= hours; // Subtract the extracted hours
127+
128+ // Extract minutes
129+ auto minutes = std::chrono::duration_cast<std::chrono::minutes>(duration);
130+ duration -= minutes; // Subtract the extracted minutes
131+
132+ // Extract seconds
133+ std::chrono::duration<double > seconds = std::chrono::duration_cast<std::chrono::duration<double >>(duration);
134+
135+ std::string ellapsedTime;
136+ if (hours.count () > 0 )
137+ ellapsedTime += std::to_string (hours.count ()) + " h " ;
138+ if (minutes.count () > 0 )
139+ ellapsedTime += std::to_string (minutes.count ()) + " m " ;
140+ std::string secondsStr{std::to_string (seconds.count ())};
141+ auto pos = secondsStr.find_first_of (' .' );
142+ if (pos != std::string::npos && (pos + 4 ) < secondsStr.size ())
143+ secondsStr.resize (pos + 4 ); // keep three decimal
144+ return (ellapsedTime + secondsStr + " s" );
144145}
0 commit comments