为什么 Python 的 I/O 比 C++ 慢得多
Why is Pythons I/O so much slower than C++?
我创建了一些用于在Python和C++中进行测试的代码,其中我从一个文件中读取两个矩阵并打印一些东西。似乎Python需要大约两倍的I/O时间:
$ ./test.sh -i Testing/2000.in -p "C++/read-write-only.out" -n 2
Executing: C++/read-write-only.out -i Testing/2000.in > TMPcurrentFileResult.out
It took 8 seconds for 2 executions
MIN: 4 seconds
MAX: 4 seconds
$ ./test.sh -i Testing/2000.in -p "python Python/read-write-only.py" -n 2
Executing: python Python/read-write-only.py -i Testing/2000.in > TMP..Results.out
It took 16 seconds for 2 executions
MIN: 8 seconds
MAX: 8 seconds
这是我用于 Python 的代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-i", dest="filename", default="bigMatrix.in",
help="input file with two matrices", metavar="FILE")
(options, args) = parser.parse_args()
def read(filename):
lines = open(filename, 'r').read().splitlines()
A = []
B = []
matrix = A
for line in lines:
if line != "":
matrix.append(map(int, line.split("t")))
else:
matrix = B
return A, B
def printMatrix(matrix):
for line in matrix:
print "t".join(map(str,line))
A, B = read(options.filename)
# Do something
printMatrix(B)
这是C++代码
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int getMatrixSize(string filename) {
string line;
ifstream infile;
infile.open (filename.c_str());
getline(infile, line);
return count(line.begin(), line.end(), 't') + 1;
}
void read(string filename, vector< vector<int> > &A, vector< vector<int> > &B){
string line;
FILE* matrixfile = freopen(filename.c_str(), "r", stdin);
int i = 0, j, a;
while (getline(cin, line) && !line.empty()) {
istringstream iss(line);
j = 0;
while (iss >> a) {
A[i][j] = a;
j++;
}
i++;
}
i = 0;
while (getline(cin, line)) {
istringstream iss(line);
j = 0;
while (iss >> a) {
B[i][j] = a;
j++;
}
i++;
}
fclose (matrixfile);
}
void printMatrix(vector< vector<int> > matrix, int n) {
for (int i=0; i < n; i++) {
for (int j=0; j < n; j++) {
if (j != 0) {
cout << "t";
}
cout << matrix[i][j];
}
cout << endl;
}
}
int main (int argc, char* argv[]) {
string filename;
if (argc < 3) {
filename = "bigMatrix.in";
} else {
filename = argv[2];
}
int n = getMatrixSize(filename);
vector<int> inner (n);
vector< vector<int> > A(n, inner), B(n, inner), C(n, inner);
read (filename, A, B);
// do something with the matrices
printMatrix(C, n);
return 0;
}
是否有可能像 I/O 一样快地获得 Python C++?如何改进 Python/C++ 的 I/O?
(我听说scanf应该比cin快。为什么它应该更快?
这是包含所有代码的 GIT 存储库。
启动 python 解释器需要一段时间。运行测试时要考虑到这一点。
当您不混合使用 C 和 C++ 文件操作例程时,应关闭与 stdio 的同步。
http://www.cplusplus.com/reference/iostream/ios_base/sync_with_stdio/
cin
和cout
通常应该比它们的 C 对应项更快(关闭同步)。
至于Python的缓慢,好吧,你为什么不检查I/O函数的实现呢?
必须缓冲任何语言的 I/O。
你为什么要比较Python和C++?
Python是一种解释型语言,而C++是编译的。
相关文章:
- 如何运行位于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