你来测测看 谁说C比Java快?
时间:2010-08-18 来源:l2010q2010
我之前在某人的博客中看到一篇文章,他比较了很多语言的运行速度,包括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 -server -XX:CompileThreshold=1 Mandelbrot 2>/dev/null |
import java.util.*; class Mandelbrot private static int iterate(float x, float y) public static void run2() public static void run() { public static void main(String args[]) { |
#include <stdio.h> #import <sys/time.h> #define BAILOUT 16 #define MAX_ITERATIONS 1000 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) { 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; 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++) { int i = mandelbrot(x/40.0, y/40.0); if (i==0) fputs("*", stderr); else fputs(" ", stderr); } } fputs("\n", stderr); } 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++) 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(); run(); } |
$ java -cp rhino1_6R5/js.jar -server -XX:CompileThreshold=1 |
$ gcc -O9 -march=pentium4 mandelbrot2.c |