我已經編寫了兩個程序,實現了一個簡單的矩陣乘法算法,一個用C++編寫,一個用Java編寫。與我的預期相反,Java程序的運行速度比C++程序快大約2.5倍。我是C++的新手,並且希望能夠在C++程序中對其進行更改以使其運行更快。性能優化:C++ vs Java沒有按預期執行
我的程序從此博客文章借用代碼和數據http://martin-thoma.com/matrix-multiplication-python-java-cpp。
下面是我使用的是當前編譯標誌:
g++ -O3 main.cc
javac Main.java
以下是當前的編譯器/運行時版本:
$ g++ --version
g++.exe (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ java -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
我的電腦是〜2012年代的酷睿i3筆記本電腦上運行窗戶MinGW的。以下是當前的性能結果:
$ time ./a.exe < ../Testing/2000.in
507584919
real 0m36.469s
user 0m0.031s
sys 0m0.030s
$ time java Main < ../Testing/2000.in
507584919
real 0m14.299s
user 0m0.031s
sys 0m0.015s
這裏是C++程序:
#include <iostream>
#include <cstdio>
using namespace std;
int *A;
int *B;
int height;
int width;
int * matMult(int A[], int B[]) {
int * C = new int[height*width];
int n = height;
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
for (int j = 0; j < n; j++) {
C[width*i+j]+=A[width*i+k] * B[width*k+j];
}
}
}
return C;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> height;
cin >> width;
A = new int[width*height];
B = new int[width*height];
for (int i = 0; i < width*height; i++) {
cin >> A[i];
}
for (int i = 0; i < width*height; i++) {
cin >> B[i];
}
int *result = matMult(A,B);
cout << result[2];
}
這裏是Java程序:
import java.util.*;
import java.io.*;
public class Main {
static int[] A;
static int[] B;
static int height;
static int width;
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
height = Integer.parseInt(reader.readLine());
width = Integer.parseInt(reader.readLine());
A=new int[width*height];
B=new int[width*height];
int index = 0;
String thisLine;
while ((thisLine = reader.readLine()) != null) {
if (thisLine.trim().equals("")) {
break;
} else {
String[] lineArray = thisLine.split("\t");
for (String number : lineArray) {
A[index] = Integer.parseInt(number);
index++;
}
}
}
index = 0;
while ((thisLine = reader.readLine()) != null) {
if (thisLine.trim().equals("")) {
break;
} else {
String[] lineArray = thisLine.split("\t");
for (String number : lineArray) {
B[index] = Integer.parseInt(number);
index++;
}
}
}
int[] result = matMult(A,B);
System.out.println(result[2]);
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static int[] matMult(int[] A, int[] B) {
int[] C = new int[height*width];
int n = height;
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
for (int j = 0; j < n; j++) {
C[width*i+j]+=A[width*i+k] * B[width*k+j];
}
}
}
return C;
}
}
下面是一個2000×2000的測試用例的鏈接: https://mega.nz/#!sglWxZqb!HBts_UlZnR4X9gZR7bG-ej3xf2A5vUv0wTDUW-kqFMA
這裏是一個2x2測試用例的鏈接:https://mega.nz/#!QwkV2SII!AtfGuxPV5bQeZtt9eHNNn36rnV4sGq0_sJzitjiFE8s
任何意見都可以解釋我在C++中做錯了什麼,或者爲什麼我的C++實現比Java運行速度慢得多,我將非常感激!
編輯:建議,我修改了程序,以便他們不實際執行乘法,但只是讀取陣列並從每個打印出一個數字。以下是這方面的表現結果。 C++程序的IO速度較慢。這只是佔了部分差異。
$ time ./IOonly.exe < ../Testing/2000.in
7
944
real 0m8.158s
user 0m0.000s
sys 0m0.046s
$ time java IOOnly < ../Testing/2000.in
7
944
real 0m1.461s
user 0m0.000s
sys 0m0.047s
您是否真的在兩種情況下測量了加載文件的時間? –
只是做到了。將發佈上述結果。 – vancan1ty
這兩個程序都看起來特別「快」。在Java代碼中,特別是使用正則表達式似乎可以保證減慢速度。總的來說,我認爲mircobenchmarks是一個壞主意。 – markspace