Measuring time in C

前端 未结 5 1686
青春惊慌失措
青春惊慌失措 2020-12-14 09:26

I\'m trying to measure some activity in C (Matrix multiplying) and noticed that I should do something like this:

clock_t start = clock();
sleep(3);
clock_t e         


        
5条回答
  •  鱼传尺愫
    2020-12-14 09:56

    If you don't care about being tied to Windows, you can try the high resolution timer. It's is a lot more precise than time(), which only has a precision of a single second because it the uses UNIX format.

    #include 
    #include 
    
    __int64 countspersec = 0;
    double secpercount = 0.0;
    __int64 starttime = 0;
    __int64 curtime = 0;
    
    int main() {
    
        // Get current time, and determine how fast the clock ticks
        QueryPerformanceCounter((LARGE_INTEGER*)&starttime);
        QueryPerformanceFrequency((LARGE_INTEGER*)&countspersec);
        secpercount = 1.0/(double)countspersec;
    
        /* calculate something */
    
        // Standard end-start stuff, account for clock speed
        QueryPerformanceCounter((LARGE_INTEGER*)&curtime);
        std::cout << "Time needed: " << (curtime-starttime)*secpercount << " sec\n";
        return 0;
    }
    

提交回复
热议问题