0

Python と C++ でテストするためのコードをいくつか作成しました。ファイルから 2 つの行列を読み取り、何かを出力します。Python は I/O に約 2 倍の時間を必要とするようです。

$ ./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 リポジトリです。

4

3 に答える 3

1

Python インタープリターの起動には時間がかかります。テストを実行するときは、これを考慮してください。

于 2012-06-29T09:05:58.630 に答える
0

C と C++ のファイル操作ルーチンを混在させない場合は、stdio との同期をオフにする必要があります。

http://www.cplusplus.com/reference/iostream/ios_base/sync_with_stdio/

cin一般に、対応する C よりも高速でcoutある必要があります (同期をオフにすると)。

Python の遅さに関して言えば、I/O 関数の実装を確認してみませんか?

于 2012-06-29T09:15:43.197 に答える
-1

どの言語でも I/O をバッファリングする必要があります。

また、なぜ Python と C++ を比較しているのですか?

Python はインタープリター言語ですが、C++ はコンパイルされます。

于 2012-06-29T09:08:54.050 に答える