G++ 和 msvc 之间的 C++ 不同文件 read/write 次
C++ different file read/write times between g++ and msvc
我有一个包含几百万行 (~80MB) 的 txt 文件。我必须在 Visual Studio 2015 年写一个程序,它读取文件的内容,对行进行排序,然后将结果写入另一个文件。在我的笔记本上有两个操作系统(Windows 7 和 Ubuntu 15.04)。
首先,我使用 g++ 在 Ubuntu 上编写了它,然后我在 Visual Studio 2015 年编译了相同的源代码。我正在测量这三个操作时间。
结果是:
Ubuntu(Ext3 分区)
- 读取:~1s
- 排序:~3.2-3.4s
- 写入:~1s
Ubuntu(运行 在 NTFS 分区上)
- 读取:~1s
- 排序:~3.2-3.4s
- 写入:~4.7s
Windows(NTFS分区)
- 读取:~5.5-6.0s(没有优化花了 2 多分钟)
- 排序:~2.6s
- 写入:~2.6-2.8s
Ubuntu:
g++ -std=c++14 main.cpp(也尝试使用 -O3 但结果相同。
Windows:
具有 -O3 优化的 msvc 编译器
在华硕 K50AB 上进行了 运行 测试。
所以我的问题是,是否有可能更接近 Ubuntu 达到的 read/write 次,或者 msvc 根本无法像 g++ 一样高效地编译代码?
另外我认为差异可能是由不同的文件系统引起的,但是在 NTFS 上从 Ubuntu 读取是相同的速度。
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;
在进行文件 io 时,有很多东西会发挥作用,例如 os 缓存、防病毒、Windows 搜索(是)。我不会将速度上的任何差异归因于 Visual Studio vs gcc 编译器。
如果您想获得更高的速度,请确保使用相当大的文件读取(我不知道您的实际文件 io 大小是多少)。
如果您想比较程序的速度,请不要在排序和文件 io 之间混合测量。文件 io 很慢。
您可以从自己的测量中看出,排序本身在您的示例中花费的时间大致相同,或者实际上使用 VS 更快。
当时从磁盘读取大块或 pos将其映射到内存中。
我有一个包含几百万行 (~80MB) 的 txt 文件。我必须在 Visual Studio 2015 年写一个程序,它读取文件的内容,对行进行排序,然后将结果写入另一个文件。在我的笔记本上有两个操作系统(Windows 7 和 Ubuntu 15.04)。 首先,我使用 g++ 在 Ubuntu 上编写了它,然后我在 Visual Studio 2015 年编译了相同的源代码。我正在测量这三个操作时间。
结果是:
Ubuntu(Ext3 分区)
- 读取:~1s
- 排序:~3.2-3.4s
- 写入:~1s
Ubuntu(运行 在 NTFS 分区上)
- 读取:~1s
- 排序:~3.2-3.4s
- 写入:~4.7s
Windows(NTFS分区)
- 读取:~5.5-6.0s(没有优化花了 2 多分钟)
- 排序:~2.6s
- 写入:~2.6-2.8s
Ubuntu: g++ -std=c++14 main.cpp(也尝试使用 -O3 但结果相同。
Windows: 具有 -O3 优化的 msvc 编译器
在华硕 K50AB 上进行了 运行 测试。
所以我的问题是,是否有可能更接近 Ubuntu 达到的 read/write 次,或者 msvc 根本无法像 g++ 一样高效地编译代码? 另外我认为差异可能是由不同的文件系统引起的,但是在 NTFS 上从 Ubuntu 读取是相同的速度。
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;
在进行文件 io 时,有很多东西会发挥作用,例如 os 缓存、防病毒、Windows 搜索(是)。我不会将速度上的任何差异归因于 Visual Studio vs gcc 编译器。
如果您想获得更高的速度,请确保使用相当大的文件读取(我不知道您的实际文件 io 大小是多少)。
如果您想比较程序的速度,请不要在排序和文件 io 之间混合测量。文件 io 很慢。
您可以从自己的测量中看出,排序本身在您的示例中花费的时间大致相同,或者实际上使用 VS 更快。
当时从磁盘读取大块或 pos将其映射到内存中。