35 c++ performance istream ostream
在http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly的 50:40时,Andrei Alexandrescu开玩笑说如何效率/慢速istream.
我过去遇到过一个问题,ostream很慢,而且fwrite明显更快(在主循环运行一次时减少了很多秒),但我从来不明白为什么也没看过它.
什么使得C++中的istream和ostream变慢?或者至少比其他东西(如fread/fget,fwrite)慢,这同样满足了需求.
Die*_*ühl 43
实际上,IOStreams不一定要慢!然而,这是以合理的方式实现它们以使它们快速的问题.大多数标准C++库似乎没有过多关注实现IOStreams.很久以前,当我的CXXRT仍然保持时,它的速度与stdio一样快 - 正确使用时!
请注意,使用IOStreams的用户几乎没有性能陷阱.以下指南适用于所有IOStream实现,尤其适用于那些快速定制的实现:
std::cin
时std::cout
,等等,你需要打电话std::sync_with_stdio(false)
!如果没有此调用,则需要使用标准流对象来与C的标准流同步.当然,在使用std::sync_with_stdio(false)
假定你不混合std::cin
使用stdin
,std::cout
以stdout
等std::endl
,因为它强制要求任何缓冲的许多不必要的刷新.同样,不要不必要地设置std::ios_base::unitbuf
或使用std::flush
.virtual
使其变得非常慢的函数.vit*_*aut 13
[i]ostreams 在设计上很慢有几个原因:
共享格式化状态:每个格式化输出操作都必须检查之前可能已被 I/O 操纵器改变的所有格式化状态。出于这个原因,iostreams 本质上比printf
-like API慢(特别是使用格式字符串编译,如 Rust 或{fmt}避免解析开销),其中所有格式信息都是本地的。
不受控制地使用语言环境:即使您不希望这样,所有格式都会通过低效的语言环境层,例如在编写 JSON 文件时。请参阅N4412:iostreams 的缺点。
低效的代码生成:使用 iostream 格式化消息通常由多个函数调用组成,因为参数和 I/O 操纵器与消息的部分交错。例如,有三个函数调用(godbolt)在
std::cout << "The answer is " << answer << ".\n";
Run Code Online (Sandbox Code Playgroud)
与等效调用中的一个(Godbolt)相比printf
:
printf("The answer is %d.\n", answer);
Run Code Online (Sandbox Code Playgroud)
额外的缓冲和同步。这可以禁用,sync_with_stdio(false)
代价是与其他 I/O 设施的互操作性较差。
也许这可以让你知道你正在处理什么:
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <fstream>
#include <time.h>
#include <string>
#include <algorithm>
unsigned count1(FILE *infile, char c) {
int ch;
unsigned count = 0;
while (EOF != (ch=getc(infile)))
if (ch == c)
++count;
return count;
}
unsigned int count2(FILE *infile, char c) {
static char buffer[8192];
int size;
unsigned int count = 0;
while (0 < (size = fread(buffer, 1, sizeof(buffer), infile)))
for (int i=0; i<size; i++)
if (buffer[i] == c)
++count;
return count;
}
unsigned count3(std::istream &infile, char c) {
return std::count(std::istreambuf_iterator<char>(infile),
std::istreambuf_iterator<char>(), c);
}
unsigned count4(std::istream &infile, char c) {
return std::count(std::istream_iterator<char>(infile),
std::istream_iterator<char>(), c);
}
unsigned int count5(std::istream &infile, char c) {
static char buffer[8192];
unsigned int count = 0;
while (infile.read(buffer, sizeof(buffer)))
count += std::count(buffer, buffer+infile.gcount(), c);
count += std::count(buffer, buffer+infile.gcount(), c);
return count;
}
unsigned count6(std::istream &infile, char c) {
unsigned int count = 0;
char ch;
while (infile >> ch)
if (ch == c)
++count;
return count;
}
template <class F, class T>
void timer(F f, T &t, std::string const &title) {
unsigned count;
clock_t start = clock();
count = f(t, 'N');
clock_t stop = clock();
std::cout << std::left << std::setw(30) << title << "\tCount: " << count;
std::cout << "\tTime: " << double(stop-start)/CLOCKS_PER_SEC << "\n";
}
int main() {
char const *name = "equivs2.txt";
FILE *infile=fopen(name, "r");
timer(count1, infile, "ignore");
rewind(infile);
timer(count1, infile, "using getc");
rewind(infile);
timer(count2, infile, "using fread");
fclose(infile);
std::ifstream in2(name);
timer(count3, in2, "ignore");
in2.clear();
in2.seekg(0);
timer(count3, in2, "using streambuf iterators");
in2.clear();
in2.seekg(0);
timer(count4, in2, "using stream iterators");
in2.clear();
in2.seekg(0);
timer(count5, in2, "using istream::read");
in2.clear();
in2.seekg(0);
timer(count6, in2, "using operator>>");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行这个,我得到这样的结果(使用MS VC++):
ignore Count: 1300 Time: 0.309
using getc Count: 1300 Time: 0.308
using fread Count: 1300 Time: 0.028
ignore Count: 1300 Time: 0.091
using streambuf iterators Count: 1300 Time: 0.091
using stream iterators Count: 1300 Time: 0.613
using istream::read Count: 1300 Time: 0.028
using operator>> Count: 1300 Time: 0.619
Run Code Online (Sandbox Code Playgroud)
这个(与MinGW):
ignore Count: 1300 Time: 0.052
using getc Count: 1300 Time: 0.044
using fread Count: 1300 Time: 0.036
ignore Count: 1300 Time: 0.068
using streambuf iterators Count: 1300 Time: 0.068
using stream iterators Count: 1300 Time: 0.131
using istream::read Count: 1300 Time: 0.037
using operator>> Count: 1300 Time: 0.121
Run Code Online (Sandbox Code Playgroud)
正如我们在结果中看到的那样,这并不是因为iostream明显缓慢.相反,很大程度上取决于你如何使用iostream(以及在较小程度上FILE *
).这些实现之间也存在相当大的差异.
尽管如此,每个(fread
和istream::read
)的最快版本基本上是捆绑的.使用VC++ getc
比任何一个istream::read
或更慢istreambuf_iterator
.
一句话:从iostreams获得良好的性能需要更多的关注而不是FILE *
- 但它肯定是可能的.它们还为您提供了更多选择:当您不关心速度时的便利性,以及与C风格I/O最佳匹配的性能,以及一些额外的工作.
归档时间: |
|
查看次数: |
11696 次 |
最近记录: |