Best timing method in C?

前端 未结 6 990
广开言路
广开言路 2020-11-28 18:55

What is the best way to time a code section with high resolution and portability?

/* Time from here */
ProcessIntenseFunction();
/* to here. */

printf(\"Tim         


        
6条回答
  •  旧时难觅i
    2020-11-28 19:32

    If you don't want CPU time then I think what you're looking for is the timeval struct.

    I use the below for calculating execution time:

    int timeval_subtract(struct timeval *result,                                                                                                                                        
                         struct timeval end,                                                                                                                                                 
                         struct timeval start)                                                                                                                                               
    {                                                                                                                                                                                   
            if (start.tv_usec < end.tv_usec) {                                                                                                                                          
                    int nsec = (end.tv_usec - start.tv_usec) / 1000000 + 1;                                                                                                             
                    end.tv_usec -= 1000000 * nsec;                                                                                                                                      
                    end.tv_sec += nsec;                                                                                                                                                 
            }                                                                                                                                                                           
            if (start.tv_usec - end.tv_usec > 1000000) {                                                                                                                                
                    int nsec = (end.tv_usec - start.tv_usec) / 1000000;                                                                                                                 
                    end.tv_usec += 1000000 * nsec;                                                                                                                                      
                    end.tv_sec -= nsec;                                                                                                                                                 
            }                                                                                                                                                                           
    
            result->tv_sec = end.tv_sec - start.tv_sec;                                                                                                                                 
            result->tv_usec = end.tv_usec - start.tv_usec;                                                                                                                              
    
            return end.tv_sec < start.tv_sec;                                                                                                                                           
    }                                                                                                                                                                                   
    
    void set_exec_time(int end)                                                                                                                                                         
    {                                                                                                                                                                                   
            static struct timeval time_start;                                                                                                                                           
            struct timeval time_end;                                                                                                                                                    
            struct timeval time_diff;                                                                                                                                                   
    
            if (end) {                                                                                                                                                                  
                    gettimeofday(&time_end, NULL);                                                                                                                                      
                    if (timeval_subtract(&time_diff, time_end, time_start) == 0) {                                                                                                      
                            if (end == 1)                                                                                                                                               
                                    printf("\nexec time: %1.2fs\n",                                                                                                                     
                                            time_diff.tv_sec + (time_diff.tv_usec / 1000000.0f));                                                                                       
                            else if (end == 2)                                                                                                                                          
                                    printf("%1.2fs",                                                                                                                                    
                                            time_diff.tv_sec + (time_diff.tv_usec / 1000000.0f));                                                                                       
                    }                                                                                                                                                                   
                    return;                                                                                                                                                             
            }                                                                                                                                                                           
            gettimeofday(&time_start, NULL);                                                                                                                                            
    }                                                                                                                                                                                   
    
    void start_exec_timer()                                                                                                                                                             
    {                                                                                                                                                                                   
            set_exec_time(0);                                                                                                                                                           
    }                                                                                                                                                                                   
    
    void print_exec_timer()                                                                                                                                                             
    {                                                                                                                                                                                   
            set_exec_time(1);                                                                                                                                                           
    }
    

提交回复
热议问题