How can I benchmark C code easily?

前端 未结 4 1002
春和景丽
春和景丽 2020-11-27 11:58

Is there a simple library to benchmark the time it takes to execute a portion of C code? What I want is something like:

int main(){
    benchmarkBegin(0);
           


        
相关标签:
4条回答
  • 2020-11-27 12:05

    In POSIX, try getrusage. The relevant argument is RUSAGE_SELF and the relevant fields are ru_utime.tv_sec and ru_utime.tv_usec.

    0 讨论(0)
  • 2020-11-27 12:07

    There may be existing utilities that help with this, but I suspect most will use some kind of sampling or possibly injection. But to get specific sections of code timed, you will probably have to add in calls to a timer like you show in your example. If you are using Windows, then the high performance timer works. I answered a similar question and showed example code that will do that. There are similar methods for Linux.

    0 讨论(0)
  • 2020-11-27 12:23

    Use the function clock() defined in time.h:

    startTime = (float)clock()/CLOCKS_PER_SEC;
    
    /* Do work */
    
    endTime = (float)clock()/CLOCKS_PER_SEC;
    
    timeElapsed = endTime - startTime;
    
    0 讨论(0)
  • 2020-11-27 12:25

    Basically, all you want is a high resolution timer. The elapsed time is of course just a difference in times and the speedup is calculated by dividing the times for each task. I have included the code for a high resolution timer that should work on at least windows and unix.

    #ifdef WIN32
    
    #include <windows.h>
    double get_time()
    {
        LARGE_INTEGER t, f;
        QueryPerformanceCounter(&t);
        QueryPerformanceFrequency(&f);
        return (double)t.QuadPart/(double)f.QuadPart;
    }
    
    #else
    
    #include <sys/time.h>
    #include <sys/resource.h>
    
    double get_time()
    {
        struct timeval t;
        struct timezone tzp;
        gettimeofday(&t, &tzp);
        return t.tv_sec + t.tv_usec*1e-6;
    }
    
    #endif
    
    0 讨论(0)
提交回复
热议问题