開発環境 Microsoft Visual C++ 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Win32 コンソール アプリケーション
プロジェクト名 duration
アプリケーションの種類 コンソール アプリケーション
追加のオプション 空のプロジェクト

処理時間の計測。

参考

duration.cpp
#include <stdio.h>
#include <time.h>
#include <set>
#include <string>
 
using namespace std;
 
#define SAMPLE 3	// サンプル数
 
double func(int loop)
{
	char *s[] = {"00", "001"};
	string str;
	clock_t start = clock();
	for (int n = 0; n < loop; n++) {
		str += s[rand() & 1];
	}
	clock_t finish = clock();
	printf("len=%d ", str.length());
	return (finish - start) / (double)CLOCKS_PER_SEC;
}
 
int main()
{
	srand((unsigned)time(NULL));
	multiset<double> set;
	for (int loop = 100000; loop <= 10000000; loop *= 10) {
		printf("ループ数=%d\n", loop);
		set.clear();
		for (int n = 0; n < SAMPLE; n++) {
			double duration = func(loop);
			set.insert(duration);
			printf("%.2fs\n", duration);
		}
 
		multiset<double>::iterator it = set.begin();
		for (int n = 0; n < SAMPLE / 2; n++) {
			it++;
		}
		printf("中央値=%.2fs\n", (*it));
	}
	return 0;
}
 

出力
ループ数=100000
len=249888 0.08s
len=250168 0.06s
len=250079 0.08s
中央値=0.08s
ループ数=1000000
len=2499818 0.69s
len=2500082 0.70s
len=2499997 0.69s
中央値=0.69s
ループ数=10000000
len=25000007 6.99s
len=24999854 7.00s
len=25000100 6.98s
中央値=6.99s
最終更新:2012年11月21日 12:17