以下是测试代码:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
if (argc != 2)
{
cout << "syntax: benchmark <option>" << endl << "option can be r or w" << endl;
cout << "argc: " << argc << endl;
return 1;
}
const int count = 9000000;
vector<int> data(count);
if (argv[1][0] == 'w')
{
ofstream fout("test.txt");
for (int i = 0; i < count; i++)
{
fout << i << " ";
}
}
else if (argv[1][0] == 'r')
{
ifstream fin("test.txt");
for (int i = 0; i < count; i++)
{
fin >> data[i];
}
}
}
这段代码会自动生成 test.txt ,其中是 0 - 8999999 这 9000000 个数字。
注: Windows 上使用 ptime( http://www.pc-tools.net/win32/ptime/) 测试运行时间; Linux 取 time 命令返回的 real 值。
不同环境下,差异为何如此之大?特别是 Ubuntu 和 WSL ,用的 binary 都是一样的( WSL 上编译,然后复制到 Ubuntu )如何减小 Ubuntu 上运行时间?