再测JAVA性能比C/C++好问题

   今天我看了《叫嚣JAVA性能比C/C++好的人进来看看,用VC5.0编译没加优化选项的程序都比GCJ优化后生成的JAVA NATIVE CODE快!更别说用INTEL C/C++ 8.0了》

 一贴,感觉测试算法比较有意义,上测试源代码


#include "stdafx.h"
#include <stdio.h>
#include <time.h>


bool isPrime(long i)
{
	for(long test = 2; test < i; test++)
	{
		if(i%test == 0)
		{
			return false;
		}
	}
	return true;
}


void main(void)
{
	clock_t start_time = clock();

	long n_loops =  50000;
	long n_primes = 0;

	for(long i = 0; i < n_loops; i++)
	{
		if(isPrime(i))
		{
			n_primes++;
		}
	}

	clock_t end_time = clock();
	printf("%d primes found\n", n_primes);
	printf("Time taken = %d\n", end_time - start_time);
	getchar();
}


package prime;

import java.io.*;

class prime

{

   private static boolean isPrime(long i)

   {

       for(long test = 2; test < i; test++)

       {

       if(i%test == 0)

       {

        return false;

       }

       }       

       return true;

   }



   public static void main(String[] args) throws IOException

   {

       long start_time = System.currentTimeMillis();



       long n_loops =  50000;

       long n_primes = 0;



       for(long i = 0; i < n_loops; i++)

       {

       if(isPrime(i))

           {

           n_primes++;

           }

       }

   

       long end_time = System.currentTimeMillis();



       System.out.println(n_primes + " primes found");

       System.out.println("Time taken = " + (end_time - start_time));

   }

}


结果:

VC 2012 编译运行结果:
x86
primerelease
5135 primes found
Time taken = 343


5135 primes found
Time taken = 343



x64
primereleasex64
5135 primes found
Time taken = 347


5135 primes found
Time taken = 348


5135 primes found
Time taken = 348


JAVA 运行结果

C:\Users\luozhuang>java -jar G:\test.jar
5135 primes found
Time taken = 1253


C:\Users\luozhuang>java -jar G:\test.jar
5135 primes found
Time taken = 1229


C:\Users\luozhuang>java -jar G:\test.jar
5135 primes found
Time taken = 1257


C:\Users\luozhuang>java -jar G:\test.jar
5135 primes found
Time taken = 1249

机器平台:


C:\Users\luozhuang>java -version
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)


C++编译器

vc++ 2012 分x86 和x64两个平台


系统 windows 8 x64

i7 3630qm + 8G 内存

你可能感兴趣的:(java,C++,性能)