fri*_*ist 5 c++ getline ifstream
我正在尝试比较逐行读取文件的性能。第一种情况是针对 string 和 istream 的 getline,第二种情况是针对 char* 和 FILE* 的 getline。我在想:
考虑下面的输出(首先是 ifstream):
Lines count: 10628126
ifstream getline: 43.2684
Lines count: 10628126
fopen getline: 1.06217
Run Code Online (Sandbox Code Playgroud)
首先是文件*:
Lines count: 10628126
fopen getline: 1.96065
Lines count: 10628126
ifstream getline: 43.0428
Run Code Online (Sandbox Code Playgroud)
我用于测试的代码:
#include <fstream>
#include <iostream>
#include <string>
#include <sys/time.h>
#include <stdio.h>
using namespace std;
double gettime()
{
double result = 0;
struct timeval tv = {0};
struct timezone tz = {0};
gettimeofday(&tv, &tz);
result = tv.tv_sec + (1.0 * tv.tv_usec / 1000000);
return result;
}
void read_cpp(const char * filename)
{
ifstream ifile(filename);
string line;
unsigned int i = 0;
while(getline(ifile, line)) i++;
cout << "Lines count: " << i << endl;
}
void read_c(const char * filename)
{
FILE * ifile = fopen(filename, "r");
size_t linesz = 4096+1;
char * line = new char[linesz];
unsigned int i = 0;
while(getline(&line, &linesz, ifile) > 0) i++;
delete[] line;
cout << "Lines count: " << i << endl;
fclose(ifile);
}
int main(int argc, char * argv[])
{
double tmstart;
tmstart = gettime();
read_cpp(argv[1]);
cout << "ifstream getline: " << (gettime() - tmstart) << endl;
tmstart = gettime();
read_c(argv[1]);
cout << "fopen getline: " << (gettime() - tmstart) << endl;
}
Run Code Online (Sandbox Code Playgroud)
PS 我尝试交换 read_cpp 和 read_c 几乎没有区别。
更新
看来@Galik和@geza无法使用g++编译器重现该问题,所以我检查了linux环境下的代码,C和C++实现之间几乎没有区别。所以看来是环境问题。最初我使用 Mac OS X 和默认的 C++ 编译器测量了时间clang(令我惊讶):
$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Run Code Online (Sandbox Code Playgroud)
但所有这些事情在 real 上都从未发生过g++:
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ...
Thread model: posix
gcc version 4.9.2 (Debian 4.9.2-10)
Run Code Online (Sandbox Code Playgroud)
抱歉给大家带来不便。
更新2
我发现相关主题clang++ fstreams 比 g++ 慢 10 倍。作者还面临着 clang 编译的代码性能下降的问题。要解决此问题,可以使用不同的stdlib实现 ( -stdlib=stdlibc++) 而不是默认的实现 ( -stdlib=libc++)。在这种情况下clang将显示弃用警告:
clang: warning: libstdc++ is deprecated; move to libc++ [-Wdeprecated]
Run Code Online (Sandbox Code Playgroud)
但性能会好得多(即使没有优化):
Lines count: 10628126
fopen getline: 1.02899
Lines count: 10628126
ifstream getline: 1.67594
Run Code Online (Sandbox Code Playgroud)
C++ 版本做了更多的边界检查、区域设置解释和 iostream 状态管理。它非常坚固。
c 版本是极简的并且更脆弱。
安全性和实用性是有代价的。
这个代价就是时间。
更新:
c readline期望使用malloc和free,而不是new和delete。
这是更正后的版本:
#include <cstdlib>
#include <cstdio>
#include <iostream>
void read_c(const char * filename)
{
FILE * ifile = fopen(filename, "r");
size_t linesz = 0;
char * line = nullptr;
unsigned int i = 0;
while(getline(&line, &linesz, ifile) > 0) i++;
free(line);
std::cout << "Lines count: " << i << std::endl;
fclose(ifile);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8179 次 |
| 最近记录: |