2006年10月03日
スレッド生成コスト
計測してみた。
環境
dylan% uname -a Linux dylan 2.6.15-23-386 #1 PREEMPT Tue May 23 13:49:40 UTC 2006 i686 GNU/Linuxd glibc 2.3.6
コード
double
gettimeofday_sec()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (double)tv.tv_usec * 1e-6;
}
static void*
func(void* arg)
{
return NULL;
}
int main(int argc, char **argv)
{
double t = gettimeofday_sec();
int i;
for (i = 0; i < atoi(argv[1]); i++) {
pthread_t th;
pthread_create(&th, NULL, func, NULL);
}
printf("time = %f\n", gettimeofday_sec() - t);
}
実行結果
dylan% gcc -lpthread test.c; ./a.out 1
time = 0.000744
dylan% gcc -lpthread test.c; ./a.out 10
time = 0.003024
dylan% gcc -lpthread test.c; ./a.out 100
time = 0.036090
dylan% gcc -lpthread test.c; ./a.out 1000
time = 0.188237
dylan% gcc -lpthread test.c; ./a.out 10000
time = 0.578391
dylan% gcc -lpthread test.c; ./a.out 100000
time = 4.923276
検索自体は遅くても0.2秒以内で終わるので1000台規模にならないとそこがボトルネックになる事はなさそう。まぁそんな規模のクラスタ持ってる所はほとんど無いので、今のアーキテクチャでも大丈夫っぽいな。
- by
- at 11:40

comments
linuxの場合、pthread_attr_set_guardsize()に0を指定してguard areaを作らないようにすることで、そのベンチマークより一桁は良い数字が出ると思います。
(実用的な議論は別途必要だと思いますけど)
なんと。情報有難うございます。ちょっと試してみます。
しかし一桁も良い数字が出るのだったら、この単純なモデルで十分スケーラビリティが確保出来ていますね。なんか愚直だ...。