이번에는 단일 문자 출력을 비교해본다.
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 | #include "stdafx.h" // iostream, cstdio, chrono, Windows.h int main(){ using std::cout; using std::endl; using std::ios_base; using std::chrono::system_clock; using std::chrono::duration_cast; using std::chrono::nanoseconds; char c= 'H'; system_clock::time_point start; system_clock::time_point end; start= system_clock::now(); for (int n = 0; n < 100; n++) printf("%c",c); end = system_clock::now(); auto printf_time = duration_cast(end - start).count(); start = system_clock::now(); for (int n = 0; n < 100; n++) putchar(c); end = system_clock::now(); auto putchar_time = duration_cast(end - start).count(); start = system_clock::now(); for (int n = 0; n < 100; n++) std::cout << c; end = system_clock::now(); auto cout_time = duration_cast(end - start).count(); start = system_clock::now(); for (int n = 0; n < 100; n++) std::cout.put(c); end = system_clock::now(); auto coutput_time = duration_cast(end - start).count(); ios_base::sync_with_stdio(false); start = system_clock::now(); for (int n = 0; n < 100; n++) std::cout << c; end = system_clock::now(); auto nosync_time = duration_cast(end - start).count(); start = system_clock::now(); for (int n = 0; n < 100; n++) std::cout.put(c); end = system_clock::now(); auto nosync_put_time = duration_cast(end - start).count(); system("cls"); std::cout << "printf : " << printf_time << "nanoseconds" << endl << "putchar : " << putchar_time << "nanoseconds" << endl << "cout : " << cout_time << "nanoseconds" << endl << "cout.put : " << coutput_time << "nanoseconds" << endl << "no sync cout : " << nosync_time << "nanoseconds" << endl << "no sync cout.put : " << nosync_put_time << "nanoseconds" << endl; return 0;}Colored by Color Scripter | cs |
??? 결과가 이상하다.
putchar이 구조가 단순하니까 printf보다 더 빨라야 하는 거 아닌가?
그리고 무슨 조화인지 싱크를 끊은 cout가 더 느리게 나왔다.
너무 짧아서 그런가 해서 반복횟수를 500으로 늘리고 다시 돌려봤다.
표본이 너무 적어서 그랬나? 다시 돌려보니까 뭔가 분포가 제멋대로다.
5000번은 돌려야겠다.
5000번을 돌리니까 비슷비슷해졌다..
그냥 문자출력은 편한걸로 골라쓰면 될것같다.