ios vs printf
昨日ストリームの話をした時に、速度の話をしたんだけど、実際どれほど違うのか実験してみた。
#include "stdafx.h" #include#include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { float cTime, cppTime; int numTests = 1000; boost::timer c; for ( int i = 0; i < numTests; ++i ) { printf( "hello world%d %f %x", i, 5.0f, i ); } cTime = c.elapsed(); boost::timer cpp; for ( int i = 0; i < numTests; ++i ) { cout << "hello world" << i << 5.0f << hex( cout ) << i << dec( cout ); } cppTime = cpp.elapsed(); cout << endl; cout << "[ result ]\n"; cout << "printf : " << cTime << endl; cout << "cout : " << cppTime << endl; char ch; cin >> ch; return 0; }
そして、結果@VC7.1は、
... [ result ] printf : 0.125 cout : 1.125
10 倍ですか...。ロケールとか余計なものがなくなれば実行時の文字列解析を要する printf よりも、コンパイル時決定の ios の方が速い筈なのに...。うむ。昔からの夢、「ロケール無しの高速ストリーム」が作りたくなってきた。