logo

Navigation:First_Community->Software->Java Goto:New TopicSettingSearch
你来测测看 谁说C比Java快?
Posted by: maomao Date: April 25, 2008 09:58PM

MegaEntry - Social networking and discussion site!

我之前在某人的博客中看到一篇文章,他比较了很多语言的运行速度,包括Ruby、Io、 PHP、Python、Lua、Java、Perl、Applescript、TCL、ELispe、Javascript、OCaml、Ghostscript和C等。数据如下:

语言

时间

相对速度

ocaml compiled 3.09.2

0.05 seconds

1.00 x

SBCL 1.0.2

0.13 seconds

2.43 x

C gcc-4.0.1

0.14 seconds

2.67 x

Java 1.4.2

0.39 seconds

7.49 x

Lua 5.1

1.25 seconds

23.81 x

Io 20070410 Vector

1.37 seconds

26.13 x

ocaml bytecode 3.09.2

3.75 seconds

71.48 x

Python 2.5.1

9.99 seconds

190.33 x

Ghostscript 8.51

11.79 seconds

224.51 x

Perl 5.8.6 Optimized

12.37 seconds

235.57 x

TCL 8.4 Optimized

16.00 seconds

304.76 x

Perl 5.8.6

21.75 seconds

414.29 x

PHP 5.1.4

23.10 seconds

440.05 x

Javascript SpiderMonkey v1.6

31.14 seconds

593.10 x

Ruby 1.8.4

33.05 seconds

629.54 x

Emacs Lisp

47.00 seconds

895.24 x

Applescript

71.75 seconds

1366.67 x

Io 20070410

85.44 seconds

1627.47 x

可以看到Java几乎比C慢两倍!
但是接着我发现他用的是Java的老版本而且只实验了一次,这实际上并不能够真实地体现Java的速度。
于是我迅速写了点代码运行100三次,并用我认为 “快”的方式运行(其实还有更快的,只是我比较懒没有设定)。结果如下:
$ java -server -XX:CompileThreshold=1 Mandelbrot 2>/dev/nullJava Elapsed 2.994Java Elapsed 1.926Java Elapsed 1.955
$ gcc -O8 mandelbrot.c$ ./a.out 2>/dev/nullC Elapsed 2.03C Elapsed 2.04C Elapsed 2.05
C仍然赢得了第一轮,但接下来的两次明显Java快。
当然,结果会因为代码和机器的不同而不一样,但有一点可以确定:Java运行相当快。
这次测试用的版本是Java 1.6.0—b105 和gcc 4.1.2,用的电脑是苹果,内核是酷睿双核2.33GHz ,Linux操作系统。我测试用的代码为:
Java测试用的代码
import java.util.*; 
class Mandelbrot

CopyRight owned by the original author.--(www.MegaEntry.com)

{ static int BAILOUT = 16;static int MAX_ITERATIONS = 1000;
     private static int iterate(float x, float y){float cr = y-0.5f;float ci = x;float zi = 0.0f;float zr = 0.0f;

MegaEntry - Social networking and discussion site!

int i = 0;while (true) {i ;float temp = zr * zi;float zr2 = zr * zr;float zi2 = zi * zi;zr = zr2 - zi2 cr;zi = temp temp ci;

MegaEntry - Social networking and discussion site!

if (zi2 zr2 > BAILOUT)return i;if (i > MAX_ITERATIONS)return 0;}}
     public static void run2(){int x,y;

CopyRight owned by the original author.--(www.MegaEntry.com)

for (y = -39; y < 39; y ) {System.err.print("\n");for (x = -39; x < 39; x ) {if (iterate(x/40.0f,y/40.0f) == 0) System.err.print("*");elseSystem.err.print(" ");}

CopyRight owned by the original author.--(www.MegaEntry.com)

} }
  public static void run() {Date d1 = new Date();for (int i = 0; i < 100; i  ) run2();Date d2 = new Date();long diff = d2.getTime() - d1.getTime();System.out.println("\nJava Elapsed "   diff/1000.0f);} 
            public static void main(String args[]) {

MegaEntry - Social networking and discussion site!

run();run();run();}}
C测试用的代码
#include #import #define BAILOUT 16#define MAX_ITERATIONS 1000

MegaEntry - Social networking and discussion site!

int mandelbrot(float x, float y){float cr = y - 0.5;float ci = x;float zi = 0.0;float zr = 0.0;int i = 0;while(1) {

MegaEntry - Social networking and discussion site!

i ;float temp = zr * zi;float zr2 = zr * zr;float zi2 = zi * zi;zr = zr2 - zi2 cr;zi = temp temp ci;if (zi2 zr2 > BAILOUT)return i;

CopyRight owned by the original author.--(www.MegaEntry.com)

if (i > MAX_ITERATIONS)return 0;} }

void run2() {int x,y;for (y = -39; y < 39; y ) {fputs("\n", stderr);for (x = -39; x < 39; x ) {

MegaEntry - Social networking and discussion site!

int i = mandelbrot(x/40.0, y/40.0);if (i==0)fputs("*", stderr);elsefputs(" ", stderr);} }fputs("\n", stderr);

MegaEntry - Social networking and discussion site!

}

void run() {struct timeval aTv;gettimeofday(&aTv, NULL);long init_time = aTv.tv_sec;long init_usec = aTv.tv_usec;int i;for (i = 0; i < 100; i )

MegaEntry - Social networking and discussion site!

run2();gettimeofday(&aTv,NULL);double query_time = (aTv.tv_sec - init_time) (double)(aTv.tv_usec - init_usec)/1000000.0; printf ("C Elapsed %0.2f\n", query_time);}

int main (int argc, const char * argv[]) {run();run();

MegaEntry - Social networking and discussion site!

run();}

处于好玩,我还在Rhino编辑器上运行了JS的测试:
$ java -cp rhino1_6R5/js.jar -server -XX:CompileThreshold=1 org.mozilla.javascript.tools.shell.Main -O 9 mandelbrot.js 2>/dev/nullJavaScript Elapsed 21.95JavaScript Elapsed 17.039JavaScript Elapsed 17.466JavaScript Elapsed 17.147
在这个测试中,JS比C慢九倍。如果CPU的速度没18个月翻一番,那么2007年JS的运行速度就赶上2002 C 的速度了。
后来在C语言 的测试中加了些cpp,-march=pentium4发挥了点作用,但结果还是比Java慢:
$ gcc -O9 -march=pentium4 mandelbrot2.c$ ./a.out 2>/dev/null

CopyRight owned by the original author.--(www.MegaEntry.com)

C Elapsed 1.99C Elapsed 1.99C Elapsed 1.99

Prev:用Java解决国际化问题Next:Java编译器对于String常量表达式的优化

Reply To This Message
Subject: 

IE 6.0 or above is perfect