我想比较使用Python和C++从标准输入读取字符串输入的行,并且惊讶地看到我的C++代码比等效的Python代码慢一个数量级。由于我的C++生疏并且我还不是Pythonista专家,请告诉我是否做错了什么或者是否误解了什么。
(TLDR答案:包含语句:cin.sync_with_stdio(false)
或只使用fgets
代替。
TLDR结果:一直滚动到我问题的底部,看看表格。)
C++代码:
#include <iostream>#include <time.h>
using namespace std;
int main() {string input_line;long line_count = 0;time_t start = time(NULL);int sec;int lps;
while (cin) {getline(cin, input_line);if (!cin.eof())line_count++;};
sec = (int) time(NULL) - start;cerr << "Read " << line_count << " lines in " << sec << " seconds.";if (sec > 0) {lps = line_count / sec;cerr << " LPS: " << lps << endl;} elsecerr << endl;return 0;}
// Compiled with:// g++ -O3 -o readline_test_cpp foo.cpp
Python等效:
#!/usr/bin/env pythonimport timeimport sys
count = 0start = time.time()
for line in sys.stdin:count += 1
delta_sec = int(time.time() - start_time)if delta_sec >= 0:lines_per_sec = int(round(count/delta_sec))print("Read {0} lines in {1} seconds. LPS: {2}".format(count, delta_sec,lines_per_sec))
以下是我的成果:
$ cat test_lines | ./readline_test_cppRead 5570000 lines in 9 seconds. LPS: 618889
$ cat test_lines | ./readline_test.pyRead 5570000 lines in 1 seconds. LPS: 5570000
我应该注意的是,我在Mac OS X v10.6.8(Snow Leopard)和Linux2.6.32(Red HatLinux6.2)下都试过这个。前者是MacBook Pro,后者是一个非常强大的服务器,并不是说这太相关了。
$ for i in {1..5}; do echo "Test run $i at `date`"; echo -n "CPP:"; cat test_lines | ./readline_test_cpp ; echo -n "Python:"; cat test_lines | ./readline_test.py ; done
Test run 1 at Mon Feb 20 21:29:28 EST 2012CPP: Read 5570001 lines in 9 seconds. LPS: 618889Python:Read 5570000 lines in 1 seconds. LPS: 5570000Test run 2 at Mon Feb 20 21:29:39 EST 2012CPP: Read 5570001 lines in 9 seconds. LPS: 618889Python:Read 5570000 lines in 1 seconds. LPS: 5570000Test run 3 at Mon Feb 20 21:29:50 EST 2012CPP: Read 5570001 lines in 9 seconds. LPS: 618889Python:Read 5570000 lines in 1 seconds. LPS: 5570000Test run 4 at Mon Feb 20 21:30:01 EST 2012CPP: Read 5570001 lines in 9 seconds. LPS: 618889Python:Read 5570000 lines in 1 seconds. LPS: 5570000Test run 5 at Mon Feb 20 21:30:11 EST 2012CPP: Read 5570001 lines in 10 seconds. LPS: 557000Python:Read 5570000 lines in 1 seconds. LPS: 5570000
微小的基准附录和总结
为了完整起见,我想我应该使用原始(同步)C++代码更新同一框中同一文件的读取速度。同样,这是针对快速磁盘上的100M行文件。以下是几种解决方案/方法的比较:
实现 | 每秒行数 |
---|---|
python(默认) | 3,571,428 |
cin(默认/天真) | 819,672 |
cin(不同步) | 12,500,000 |
fget | 14,285,714 |
wc(不公平比较) | 54,644,808 |