How to measure the time that a piece of code takes to execute?

后端 未结 4 706
说谎
说谎 2020-12-17 01:14

Suppose I want to measure the time that a certain piece of code takes. For that I would normally do something like this

clock_t startTime = clock();
//do stu         


        
相关标签:
4条回答
  • 2020-12-17 01:15

    From the Linux terminal use 'time path_to_app'

    This will return everything you want to know.

    0 讨论(0)
  • 2020-12-17 01:21

    This is why code benchmarking basically sucks- because you can't know how long it takes. Things like being pre-empted by the OS are unpredictable at best. Use a professional profiler, as they may have code in them that can deal with these problems, or don't bother. Writing clock() style things is utterly meaningless.

    0 讨论(0)
  • 2020-12-17 01:23

    I have prepared two very simple classes. The first one ProfileHelper the class populate the start time in the constructor and the end time in the destructor. The second class ProfileHelperStatistic is a container with extra statistical capability (a std::multimap + few methods to return average, standard deviation and other funny stuff).

    I have used this idea often for profiling. I guess you could make it work even in a multi-thread environment. It will require a bit of work, but I don't think it will be so difficult.

    Have a look at this question for more information C++ Benchmark tool.

    0 讨论(0)
  • 2020-12-17 01:30

    There are different ways to measure how long code takes to execute.

    If you are interested in the relative performance of certain functions, a profiler is the only way to go. Note that this will de-emphasise the impact of blocking I/O due to the computation overheads it induces.

    If you want the clock-based time of certain functions, there are loads of options.

    Personally I would say gettimeofday is sufficient.

    If you want to get precise, use RDTSC

    If you want to get really precise, you'll want something like this

    t1 = rdtsc();
    t2 = rdtsc();
    my_code();
    t3 = rdtsc();
    my_code_time = (t3-t2) - (t2-t1)
    

    You will need to repeat this block to account for thread scheduling discrepencies, and also pay attention to cacheing effects.

    0 讨论(0)
提交回复
热议问题