特定の関数を実行するのにかかる時間を計算する必要があり、次のコード(source:http://snippets.dzone.com/posts/show/4254)に遭遇しました。 "... &レコードの実行時間マイクロコードの一部」Cでtimeval構造体に必要なヘルプ
/* Put this line at the top of the file: */
#include <sys/time.h>
/* Put this right before the code you want to time: */
struct timeval timer_start, timer_end;
gettimeofday(&timer_start, NULL);
/* Put this right after the code you want to time: */
gettimeofday(&timer_end, NULL);
double timer_spent = timer_end.tv_sec - timer_start.tv_sec + (timer_end.tv_usec - timer_start.tv_usec)/1000000.0;
printf("Time spent: %.6f\n", timer_spent);
が、コードの切れ端で私の個人的な経験では、出力の時刻が "秒の代りにマイクロ秒であることを示しています。私は正直か間違っているかについて何らかのインプットが必要です(私はこれを一度だけ明確にする必要があります)。
いいえ、秒をマイクロ秒に変換するには、最初の部分に10^6を掛けなければなりません。 – sje397
@ sje397確かに。修正しました。 –