我最近用Java编写了一个计算密集型算法,然后将其翻译成C++.令我惊讶的是,C++的执行速度要慢得多.我现在编写了一个更短的Java测试程序,以及相应的C++程序 - 见下文.我的原始代码具有很多数组访问权限,测试代码也是如此.C++的执行时间要长5.5倍(请参阅每个程序末尾的注释).
结论在下面的第 1 21条评论之后......
测试代码:
g++ -o ... Java快5.5倍 g++ -O3 -o ... Java快2.9倍 g++ -fprofile-generate -march=native -O3 -o ...(运行,然后g++ -fprofile-use等)Java 1.07倍的速度.我原来的项目(比测试代码复杂得多):
Software environment:
Ubuntu 16.04 (64 bit).
Netbeans 8.2 / jdk 8u121 (java code executed inside netbeans)
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Compilation: g++ -o cpp_test cpp_test.cpp
Run Code Online (Sandbox Code Playgroud)
Java代码:
public class JavaTest {
public static void main(String[] args) {
final int ARRAY_LENGTH = 100;
final int FINISH_TRIGGER = 100000000;
int[] …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习gcc自动矢量化模块.从这里阅读文档后.
这是我尝试过的(debian jessie amd64):
$ cat ex1.c
int a[256], b[256], c[256];
foo () {
int i;
for (i=0; i<256; i++){
a[i] = b[i] + c[i];
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我只是运行:
$ gcc -x c -Ofast -msse2 -c -ftree-vectorize -fopt-info-vec-missed ex1.c
ex1.c:5:3: note: misalign = 0 bytes of ref b[i_11]
ex1.c:5:3: note: misalign = 0 bytes of ref c[i_11]
ex1.c:5:3: note: misalign = 0 bytes of ref a[i_11]
ex1.c:5:3: note: virtual phi. skip.
ex1.c:5:3: note: num. args = 4 (not unary/binary/ternary …Run Code Online (Sandbox Code Playgroud)