友人から提供されたコードのチャンクを最適化しようとしていますが、平均実行時間のベースラインが非常に不安定で、修正する理由/方法がわかりません。
コード:
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include "wall.h" /* Where his code is */
int main()
{
int average;
struct timeval tv;
int i;
for(i = 0; i < 1000; i++) /* Running his code 1,000 times */
{
gettimeofday(&tv, NULL); /* Starting time */
start(); /* Launching his code */
int ret = tv.tv_usec; /* Finishing time */
ret /= 1000; /* Converting to milliseconds */
average += ret; /* Adding to the average */
}
printf("Average execution time: %d milliseconds\n", average/1000);
return 0;
}
5 つの異なる実行の出力:
- 804 ミリ秒
- 702ミリ秒
- 394 ミリ秒
- 642 ミリ秒
- 705 ミリ秒
平均実行時間を取得するために複数の異なる方法を試しましたが、それぞれの方法で十分に正確な答えが得られないか、完全に不安定な答えが得られます。私は今何をすべきか迷っています。どんな助けでも大歓迎です!
これらのタイプのベンチマークはシステムに大きく依存することを知っているので、システムの仕様を以下にリストしました。
- Ubuntu 12.10 x64
- 7.8 GiB RAM
- Intel Core i7-3770 CPU @ 3.40GHz x 8
- GeForce GT 620/PCIe/SSE2
編集
ご意見をお寄せいただきありがとうございますが、自分で作成するのではなく、gprof を使用することにしました。ありがとう、もう一度!