Time-to-C + + recording

Source: Internet
Author: User

Http://stackoverflow.com/questions/2808398/easily-measure-elapsed-time

Https://github.com/picanumber/bureaucrat/blob/master/time_lapse.h








#include <ctime>voidF() { Using NamespaceStd; clock_t begin = Clock (); Code_to_time (); clock_t end = Clock (); double Elapsed_secs = (end - begin) / Clocks_per_ Sec; /span>

The time() function is a second of accurate to within a and there is CLOCKS_PER_SEC "clocks" within a second. This is a easy, portable measurement, even though it ' s over-simplified.

Up vote98down vote+50

You can abstract the time measuring mechanism and has each callable's run time measured with minimal extra C Ode, just by being called through a timer structure. Plus, at compile time you can parametrize the timing type (milliseconds, nanoseconds etc).

Thanks to the review by Loki Astari and the suggestion to use variadic templates. The forwarded function call.

#include <iostream>#include <chrono>Template<TypeName TimeT =Std::Chrono::Milliseconds>structMeasure{ Template<TypeNameF, TypeName ...Args> Static TypeName TimeT::Rep execution(F&&Func, Args&&amp, .....Args) { AutoStart=Std::Chrono::Steady_clock::Now();Std::Forward<Decltype(Func) > (Func)(Std::Forward<Args> (Args)...); AutoDuration=Std::Chrono::Duration_cast< TimeT> (Std::Chrono::Steady_clock::Now() -Start); return Duration.}};int Main ()  {  Std::cout <<<>::execution (functor< Span class= "pun" > (dummy << Std::endl;               /span>                

Demo

According to the comment by Howard Hinnant It's best don't to escape out of the the Chrono system until we had to. The above class could give the user the choice to call count manually by providing a extra static method (shown in C + + 14)

Template<TypeNameF, TypeName ...Args>Static AutoDuration(F&&Func, Args&&amp, .....Args){ AutoStart=Std::Chrono::Steady_clock::Now();Std::Forward<Decltype(Func) > (Func)(Std::Forward<Args> (Args)...); ReturnStd::Chrono::Duration_cast<TimeT> (Std::Chrono::Steady_clock::now ()-start); } //call. Count () Manually later when needed (eg IO) auto avg =  (measure<>::duration ( Func  + Measure< >::duration (func / 2.0    

and is most useful for clients that

"Want to post-process a bunch of durations prior to I/O (e.g. average)"

The complete code can is foundhere. My attempt to build a benchmarking tool based on Chrono are recorded here.

If c++17 ' s std::invoke is available, the invocation of the callable in could was done like this execution :

invoke(forward<decltype(func)>(func), forward<Args>(args)...);

To provide for callables that is pointers to member functions.

Time-to-C + + recording

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.