简体   繁体   English

C ++在g ++和msvc之间的文件读写时间不同

[英]C++ different file read/write times between g++ and msvc

I have a txt file with a few million rows (~80MB). 我有一个带有几百万行(〜80MB)的txt文件。 I have to write a program in Visual Studio 2015, which reads the content of the file, sorts the rows, then write the result into another file. 我必须在Visual Studio 2015中编写一个程序,该程序读取文件的内容,对行进行排序,然后将结果写入另一个文件。 On my notebook there are two operating systems(Windows 7 and Ubuntu 15.04). 我的笔记本上有两个操作系统(Windows 7和Ubuntu 15.04)。 First I wrote it on Ubuntu using g++, then I compiled the same source in Visual Studio 2015. I'm measuring these three operations times. 首先,我使用g ++在Ubuntu上编写了该代码,然后在Visual Studio 2015中编译了相同的源代码。我正在测量这三个操作时间。

The results are: 结果是:

Ubuntu(Ext3 partition) Ubuntu(Ext3分区)

  • read: ~1s 读:〜1秒
  • sort: ~3.2-3.4s 排序:〜3.2-3.4s
  • write: ~1s 写:〜1s

Ubuntu(run on NTFS partition) Ubuntu(在NTFS分区上运行)

  • read: ~1s 读:〜1秒
  • sort: ~3.2-3.4s 排序:〜3.2-3.4s
  • write: ~4.7s 写:〜4.7秒

Windows(NTFS partition) Windows(NTFS分区)

  • read: ~5.5-6.0s(without optimisation it took over 2 minutes) 读取:〜5.5-6.0s(未经优化耗时超过2分钟)
  • sort: ~2.6s 排序:〜2.6s
  • write: ~2.6-2.8s 写:〜2.6-2.8s

Ubuntu: g++ -std=c++14 main.cpp(also tried with -O3 but the results were the same. Ubuntu:g ++ -std = c ++ 14 main.cpp(也尝试了-O3,但结果是相同的。

Windows: msvc compiler with -O3 optimisation Windows:具有-O3优化功能的msvc编译器

Tests were run on Asus K50AB. 测试在华硕K50AB上进行。

So my question is that is it possible to get closer to the read/write times reached on Ubuntu, or msvc simply can't compile as efficient code as g++? 所以我的问题是,是否有可能接近Ubuntu上的读/写时间,或者msvc根本无法像g ++一样编译高效的代码? Also I thought that the differences could be caused by different filesystems, but the read from Ubuntu on NTFS was the same speed. 我还认为差异可能是由不同的文件系统引起的,但是从Ubuntu读取NTFS的速度相同。

auto t1 = std::chrono::high_resolution_clock::now();

std::ifstream is{ "rec.txt" };
std::ofstream os{ "res.txt" };

// number is the number of lines
std::vector<std::string> lines(number);

for (int i = 0; i < number; ++i)

    is >> lines[i];

auto t2 = std::chrono::high_resolution_clock::now();

std::cout << "read time: " <<
    std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count() << std::endl;


// sort elements


t1 = std::chrono::high_resolution_clock::now();

for (int i = 0; i < number; ++i)

    os << s[i] << '\n';

t2 = std::chrono::high_resolution_clock::now();

std::cout << "write time: " <<
    std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count() << std::endl;

There are a lot of things coming into play when doing file io, such as os caching, anti virus, Windows Search (yes). 做文件io时有很多事情在起作用,例如os缓存,防病毒,Windows搜索(是)。 I would not attribute any differences in speed to Visual Studio vs gcc compilers just yet. 我不会将速度的任何差异归因于Visual Studio vs gcc编译器。

If you want to get higher speeds, make sure to use reasonably large file reads (I have no idea what the actual file io sizes will be in your case). 如果要提高速度,请确保使用相当大的文件读取(我不知道实际情况下的文件io大小是多少)。

If you want to compare the speed of your program, don't mix measurements between sorting and file io. 如果要比较程序的速度,请不要在排序和文件io之间混用测量值。 File io is slooooooooow. 文件io slooooooooow。

You can see from your own measurements that the sorting itself takes approximately the same amount of time in your example or is actually faster using VS. 从您自己的测量中可以看出,在示例中,排序本身花费的时间大致相同,或者实际上使用VS可以更快。

Read large chunks at the time from disk or possibly map it into memory. 一次从磁盘读取大块或可能将其映射到内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM