C - gettimeofday for computing time?

前端 未结 5 1883
闹比i
闹比i 2020-12-08 05:13

do you know how to use gettimeofday for measuring computing time? I can measure one time by this code:

  char buffer[30];
  struct timeval tv;

  time_t curt         


        
5条回答
  •  盖世英雄少女心
    2020-12-08 05:36

    The answer offered by @Daniel Kamil Kozar is the correct answer - gettimeofday actually should not be used to measure the elapsed time. Use clock_gettime(CLOCK_MONOTONIC) instead.


    Man Pages say - The time returned by gettimeofday() is affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the system time). If you need a monotonically increasing clock, see clock_gettime(2).

    The Opengroup says - Applications should use the clock_gettime() function instead of the obsolescent gettimeofday() function.

    Everyone seems to love gettimeofday until they run into a case where it does not work or is not there (VxWorks) ... clock_gettime is fantastically awesome and portable.

    <<

提交回复
热议问题