この関数は私にとってまったく新しいものなので、慣れるために小さなプログラムを書きました。これが私のプログラムです(gettimeofdayのいくつかの呼び出し間のラップを印刷するだけです)。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
typedef struct timeval t_timeval;
t_timeval get_diff(t_timeval prev)
{
t_timeval current_time;
gettimeofday(¤t_time, NULL);
printf("diff : %ld seconds and %ld micro seconds\n",
current_time.tv_sec - prev.tv_sec, current_time.tv_usec - prev.tv_usec);
return (current_time);
}
int get_time_laps()
{
int i = 0;
t_timeval prev;
gettimeofday(&prev, NULL);
while (i++ < 50)
prev = get_diff(prev);
return (0);
}
int main()
{
get_time_laps();
return (0);
}
結果は次のとおりです。
diff : 0 seconds and 0 microseconds
diff : 0 seconds and 47 microseconds
diff : 0 seconds and 1 microseconds
diff : 0 seconds and 1 microseconds
diff : 0 seconds and 1 microseconds
diff : 0 seconds and 1 microseconds
diff : 0 seconds and 1 microseconds
diff : 0 seconds and 1 microseconds
diff : 0 seconds and 2 microseconds
diff : 0 seconds and 1 microseconds
diff : 0 seconds and 1 microseconds
diff : 0 seconds and 0 microseconds
[...]
diff : 0 seconds and 1 microseconds
では、なぜ 2 番目の差分が他の差分と大きく異なるのか疑問に思っています。参考までに、このパターンを取得するたびに、何度かテストを行いました。