为什么这种逐行文件读取在 c++ 中比在 Python 中慢得多
How can this line-by-line file reading be much slower in c++ than in Python?
我有一个2GB的文件。平均一行有 15 个字符(最多 50 个字符(。使用时:
#include <iostream>
#include <fstream>
#include <string>
void main()
{
std::ifstream input("myfile.txt");
std::string line;
while (std::getline(input, line))
{
}
return;
}
它需要 ~ 320 秒,而这个 Python 代码:
with open('myfile.txt', mode='r') as f:
for l in f:
semicolonpresent = ';' in l # do anything here, not important
不到一分钟。
我使用的C++版本有什么问题?
注意:我已经尝试了很多次,每次都是在重新启动后,或者在以前的多次运行之后(因此它可能在I/O缓存中(,但我总是得到这样的数量级。
注意2:我在Windows 7/64上编译了C++代码,使用:
call "C:Program Files (x86)Microsoft Visual Studio 12.0VCvcvarsall.bat" x86
cl main.cpp
在我的测试中,C++
并不比Python
慢。在大型二进制文件中,它们大致相同。
我稍微修改了您的代码,以提供我认为应该是准确的时间。
蟒蛇代码:
import sys
import time
start = time.time()
sum = 0
with open(sys.argv[1], mode='r') as f:
for l in f:
sum = sum + 1
end = time.time()
print "(", sum, ")", (end - start), "secs"
C++代码:
auto start = std::chrono::steady_clock::now();
std::ifstream ifs(argv[1]);
std::string line;
unsigned sum = 0;
while(std::getline(ifs, line))
++sum;
auto end = std::chrono::steady_clock::now();
auto time = double(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count());
OUT("( " << sum << " ) " << (time / 1000.0) << " secs");
处理7.1 GiB
二进制视频文件可提供:
Python: ( 32547618 ) 62.9722070694 secs
C++ : ( 32547618 ) 63.368 secs
C++
代码是在GCC v7.2
上使用以下标志编译的:
g++ -std=c++17 -O3 ...
相比之下,未优化的代码运行在68.541 secs
void main
C++无效。也不是main()
的空return
.此外,启用优化后,您的while
循环将被优化。话虽如此...
如果打开优化,您应该会注意到速度有所提高。但是,C++中的流很慢。这对C++世界来说不是新闻。那么为什么Python要快得多呢?两者并不真正等同。Python 是用 C 编写的,并在内部委托给 C 库函数。此外,Python 很可能提前分配一个缓冲区并一次性复制缓冲区。如果您想在C++中执行类似操作:
std::ifstream ifs(filename);
auto end = ifs.tellg();
ifs.seekg(0, std::ios::beg);
auto beg = ifs.tellg();
std::string buffer;
buffer.reserve(end - beg);
std::copy(std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>(),
buffer.begin());
(部分( 答案:
正如许多人在评论中提到的,cl
优化有助于:
cl /Ox helloworld1.cpp
将运行时间除以 6!
相关文章:
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- Pybind11:将元组列表从Python传递到C++
- 如何在c++中使用引用实现类似python的行为
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 递归列出所有目录中的C++与Python与Ruby的性能
- IPC使用多个管道和分支进程来运行Python程序
- 从python中调用C++函数并获取返回值
- Python 3.7 和 excess_args 的 SWIG 问题
- Python中的for循环与C++有何不同
- 使用Pybind11向Python公开Eigen::张量
- Python str to C++ to Python str
- 如何使用Python从C++中读取谷物序列化数据
- 如何在C++中使用pybind11加载一个pickle python列表
- 如何在c++中使用system()来运行包含空格的python脚本
- python集合的C++等价物是什么.计数器
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 从python调用openMP共享库时,未定义opnMP函数
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- 如何将真正的字符串从python c-api转换为python脚本
- Python ctype 'c_char_p' Memory Leak