Java的比C快? [英] Java faster than C?

查看:153
本文介绍了Java的比C快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我听到的一切都是人们说Java是通常比C慢一些,有一些例外(像涉及code,它什么都不做)。于是我去了测试。我有0和999,999之间的10万整数数组。我用了一个双循环对它们进行排序,从最小到最大,在C和Java(编译在OS X)。

结果是Java的一般都在一半的时间完成。出5次运行不同阵列的Java花了约17秒,而Ç了约32秒(这包括时间分配和填充从文件这两个是可以忽略不计的数组)。

那么,什么会作出了Java code运行比C快?是否有什么我失踪,或者一些底层技术我听说过没有?

编辑:也不能肯定是否重要,但我计时它使用time命令,没有任何自定义code

例如:$实时Java SortArray

而对于编译器选项,我不能访问命令行的权利,但它是在OS X 10.10默认的gcc选项。而我只是用默认的javac编译Java。

的gcc -o sortarray.c sortarray

javac的SortArray.java

code:

C:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;的#define SIZE 32诠释主(){
FILE *文件=的fopen(bigarray.txt,R);
INT ARRAYSIZE = 100000;
int数组[100000] = {};
INT I,J,温度;
焦炭INPUTBUFFER [SIZE]
对于(i = 0; I< ARRAYSIZE;我++){
    与fgets(INPUTBUFFER,大小,文件);
    数组[我] =的strtod(INPUTBUFFER,NULL);
}对于(i = 0; I< ARRAYSIZE;我++)
    为(J = I + 1; J< ARRAYSIZE; J ++)
        如果(阵列[I]>阵[J]){
            INT TEMP =阵列[我]
            数组[我] =阵列[J]。
            阵列[J] =温度;
        }对于(i = 0; I< ARRAYSIZE;我++)
    的printf(%d个,数组[我]);返回0;
}

Java的:

 进口java.io. *;
进口java.util.Scanner中;公共类SortArray {
公共静态无效的主要(字串[] args)抛出IOException
    的System.out.println(世界,你好);
    扫描器S =新的扫描仪(新的文件(bigarray.txt));    INT []数组=新INT [100000]。
    的for(int i = 0; I< 100000;我++){
        数组[我] = s.nextInt();
    }
    的for(int i = 0; I< array.length,我++){
        对于(INT J = I + 1; J< array.length; J ++){
            如果(阵列[I]>阵[J]){
                INT TEMP =阵列[我]
                数组[我] =阵列[J]。
                阵列[J] =温度;
            }
        }
    }    的for(int i = 0; I< array.length,我++){
        System.out.print(数组[I] +);
    }}
}


解决方案

您自己的运行时间进行比较是不公平的和有缺陷的。您可以使用默认的编译器选项,但Java和C对他们能有什么优化,默认情况下有所不同。并在不优化总体性能测试开启是完全无用的本身,输出完全可以在现实世界中,这取决于改变可以被优化了。

Java使用JIT,而它使用默认选项甚至可以运行来优化您的code。

有关GCC默认的优化选项 -O0 这意味着有没有在所有做任何优化。尝试使用 -O2 -O3 一个更公平的比较。

有关我的机器上的例子的结果是:


  • C code。与默认的编译器选项= 12.5secs。

  • C $ C $与 -O3 C才能允许优化= 4.5secs。

  • 的Java code = 8.5秒。

Everything I had heard so far is people saying Java is generally slower than C, with some exceptions (Like involving code that does nothing). So I went out to test it. I had an array of 100,000 integers between 0 and 999,999. I used a double for loop to sort them from smallest to largest, in both C and java (compiled on OS X).

The results were Java generally finishing in half the time. Out of 5 runs with different arrays Java took around 17 seconds while C took around 32 seconds (That includes the time allocating and populating the array from a file which for both was negligible).

So what would have made the Java code run faster than C? Is there something I'm missing, or some underlying technology I've heard nothing about?

Edit: Also not sure if it matters, but I timed it using the time command, not any custom code.

Ex: $time java SortArray

And as for compiler options, I can't access that command line right now, but it was default gcc options on OS X 10.10. And I just used default javac to compile the Java.

gcc sortarray.c -o sortarray

javac SortArray.java

Code:

C:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 32

int main() {
FILE *file = fopen("bigarray.txt", "r");
int arraySize = 100000;
int array[100000] = {};
int i, j, temp;
char inputBuffer[SIZE];
for (i=0; i<arraySize; i++) {
    fgets(inputBuffer, SIZE, file);
    array[i] = strtod(inputBuffer, NULL);
}

for (i=0; i<arraySize; i++)
    for (j=i+1; j<arraySize; j++)
        if (array[i] > array[j]) {
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }

for (i=0; i<arraySize; i++)
    printf("%d, ", array[i]);

return 0;
}

Java:

import java.io.*;
import java.util.Scanner;

public class SortArray {
public static void main(String[] args) throws IOException {
    System.out.println("Hello world");
    Scanner s = new Scanner(new File("bigarray.txt"));

    int[] array = new int[100000];
    for(int i=0; i<100000; i++) {
        array[i] = s.nextInt();
    }


    for(int i=0; i<array.length; i++) {
        for(int j=i+1; j<array.length; j++) {
            if (array[i] > array[j]) {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    for(int i=0; i<array.length; i++) {
        System.out.print(array[i] + ", ");
    }

}
}

解决方案

Your comparison of their runtime is unfair and flawed. You use default compiler options but Java and C differ on what optimizations they enable by default. And in general measuring performance without optimizations turned on is completely useless of itself, the output could completely change in the real world depending on which can be optimized more.

Java uses a JIT to optimize your code while it runs even with default options.

For gcc the default optimization option is -O0 which means there weren't any optimizations done at all. Try using -O2 or -O3 for a more fair comparison.

For example results on my machine are:

  • C code with default compiler options = 12.5secs.
  • C code with -O3 to enable optimization = 4.5secs.
  • Java code = 8.5 secs.

这篇关于Java的比C快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆