我正在尝试比较逐行读取文件的性能。第一种情况是针对 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 …Run Code Online (Sandbox Code Playgroud)