输出流效率测试
来源:互联网 发布:剑与魔法翅膀升阶数据 编辑:程序博客网 时间:2024/06/03 07:52
这个问题Effective STL中有提过。今天编程时由于涉及到输出的效率,加上闲着没事,所以做了这测试。
测试对象:
1.cout<<
2.ostream_iterator
3.ostreambuf_iterator
4.printf
5.cout.put
6.rdbuf()->sputn
测试平台:
1.6GHz处理器
测试代码:
#include <iostream>#include <iterator>#include <cstdio>#include <ctime>#include <iomanip>using namespace std;int main(){ /*以10000个'a'作为输出目标*/ char a[10000] = {'a'}; for (char& x : a) { x = 'a'; } clock_t start1, end1, start2, end2, start3, end3, start4, end4, start5, end5, start6, end6; /*测试ostream_iterator*/ start1 = clock(); copy(a, a + 10000, ostream_iterator<char>(cout)); end1 = clock(); /*测试ostreambuf_iterator*/ start2 = clock(); copy(a, a + 10000, ostreambuf_iterator<char>(cout)); end2 = clock(); /*测试cout<<*/ start3 = clock(); for (char x: a) cout << x; end3 = clock(); /*测试printf*/ start4 = clock(); for (char x: a) printf("%c", x); end4 = clock(); /*测试cout.put*/ start5 = clock(); for (char x: a) cout.put(x); end5 = clock(); /*测试rdbuf()->sputn*/ start6 = clock(); cout.rdbuf()->sputn(a, 10000); end6 = clock(); cout << endl; cout << setw(25) << "ostream_iterator: " << static_cast<double>(end1 - start1) / CLOCKS_PER_SEC << " seconds"<< endl; cout << setw(25) << "ostreambuf_iterator: " << static_cast<double>(end2 - start2) / CLOCKS_PER_SEC << " seconds"<< endl; cout << setw(25) << "cout<<: " << static_cast<double>(end3 - start3) / CLOCKS_PER_SEC << " seconds"<< endl; cout << setw(25) << "printf: " << static_cast<double>(end4 - start4) / CLOCKS_PER_SEC << " seconds"<< endl; cout << setw(25) << "cout.put: " << static_cast<double>(end5 - start5) / CLOCKS_PER_SEC << " seconds"<< endl; cout << setw(25) << "rdbuf()->sputn: " << static_cast<double>(end6 - start6) / CLOCKS_PER_SEC << " seconds"<< endl; return 0;}
测试结果:
1.用MinGW编译
ostream_iterator:5.806 secondsostreambuf_iterator:0.142 secondscout<<:5.812 secondsprintf:5.673 secondscout.put:5.752 secondsrdbuf()->sputn:0.118 seconds2.用Borland C++编译
ostream_iterator:5.569 secondsostreambuf_iterator:0.203 secondscout<<:4.789 secondsprintf:4.493 secondscout.put:4.555 secondsrdbuf()->sputn:0.141 seconds3.用VS2012编译
ostream_iterator:4.809 secondsostreambuf_iterator:4.659secondscout<<:4.956 secondsprintf:5.848 secondscout.put:6.976 secondsrdbuf()->sputn:6.793 seconds果然,用ostreambuf_iterator和rdbuf()->sputn来进行输出的确快很多,但不同的STL实现速度提升会不同,如VS2012就提升不明显了(例外?)。
- 输出流效率测试
- 测试效率
- 测试效率
- 测试效率
- 测试效率
- 测试效率
- 测试效率
- 测试效率
- 测试效率
- 测试效率
- 测试效率
- 测试效率
- 效率测试
- 测试效率
- IO流文件复制缓冲效率测试
- 格式化输出效率
- java 异常效率测试测试
- 字符串连接效率测试
- IOS CLLocationManager和中国地图偏移(China Shift)
- linux-2.6.26内核中ARM中断实现详解
- Matrix Chain Multiplication(栈的简单应用)
- shell 创建文件夹,并在文件夹中创建空文件
- 考研小结
- 输出流效率测试
- Android警告错误搜集
- PHP学习-----文件操作/上传和下载
- 打造心中的暗黑传奇1
- iOS编译错误
- Android数据存储之SharedPreferences
- Java程序员成长之路
- soj 2302. Queue Implementation Using a Circular Array
- 设计模式之工厂方法模式