C++、C#、Java、JavaScript之间的性能比较

标题:C++、C#、Java、JavaScript之间的性能比较
Author:kagula
Date 2015-10-25


测试目的
    想知道C++语言同其它常用语言在不针对硬件手动优化之前,默认性能差距是多少,
所以写了此文。

公共环境:
[1]Windows 10 professional 64bits
[2]Core i5-2500k

C++环境:
Visual Studio 2013 Update5
c++ console project(32位)
Platform Toolset: Visual Studio 2013 - Windows XP (v120_xp)

C#环境:
Visual Studio 2013 Update5
.Net framework 4.5
default c# console project

java环境:
jdk-7u67-windows-i586
eclipse-java-luna-SR2

JavaScript环境:
46.0.2490.71 (正式版本) m (32 位)

测试对象:
    C++、C++ 编译器性能最大优化(Maximize Speed)、C#、Java、JavaScript


测试方式:
    测试三次,取最小值,毫秒为单位。
    C++ C#分别以Release方式运行。
    Java以Run方式运行。


测试结果

C++、C#、Java、JavaScript之间的性能比较_第1张图片

数值越低越好。


总结

     证实了我一直对C++性能的看法,如果不针对硬件(例如cpu 指令集,gpu)手动优化和其它语言比起来性能优势很少。   

    Microsoft compiler的自动优化很厉害,优化后,递归既然只用了0ms就返回了。

    让我意外的是java的性能比C#还强。

附:
测试用的源代码
C++源代码
// FibonacciSequence.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <time.h>

#include <iostream>
using namespace std;

int FibonacciSequence(int item)
{
	if (item == 0)
		return 0;
	else if (item == 1)
		return 1;

	return FibonacciSequence(item - 1) + FibonacciSequence(item - 2);
}

/*
30=>66nm
39=>4928nm
*/

void test1()
{
	clock_t t1 = clock();
	int nR = FibonacciSequence(39);
	clock_t t2 = clock();

	cout << "FibonacciSequence = " << nR << " TotalTime:" << t2 - t1 << "ms" << endl;
}

void prepareData(int count, int ***pppMatrix)
{
	int **ppMatrix = new int*[count];
	*pppMatrix = ppMatrix;
	for (int row = 0; row < count; row++){
		ppMatrix[row] = new int[count];
		for (int col = 0; col < count; col++)
			ppMatrix[row][col] = col;
	}
}

void ReleaseData(int count, int **ppMatrix)
{
	//for (int row = 0; row < count; row++){
	//	delete[] ppMatrix[row];
	//}
	delete[] ppMatrix;
	ppMatrix = nullptr;
}

void matrixMultiplication(int count,int **ppA, int **ppB, int ***pppR){
	int **ppMatrix = new int*[count];
	*pppR = ppMatrix;

	for (int row = 0; row < count; row++){
		ppMatrix[row] = new int[count];
	}

	//
	for (int row = 0; row < count;row++)
	{
		for (int col = 0; col < count;col++)
		{
			ppMatrix[row][col] = 0;
			for (int k = 0; k < count;k++)
			{
				ppMatrix[row][col] += ppA[row][col] * ppB[col][row];
			}
		}
	}
}

void test2()
{
	int **a = nullptr;
	int **b = nullptr;
	int **r;
	int count = 500;
	prepareData(count, &a);
	prepareData(count, &b);

	for (int i = 0; i < 3;i++)
	{
		clock_t t1 = clock();

		matrixMultiplication(count, a, b, &r);
		delete[] r;

		clock_t t2 = clock();
		cout << "matrixMultiplication =>" << t2 - t1 << "ms" << endl;
	}

	ReleaseData(count, a);
	ReleaseData(count, b);
}

int _tmain(int argc, _TCHAR* argv[])
{
	//for (int i = 0; i < 3; i++)
	//	test1();

	test2();

	return 0;
}



C#源代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InCSharp
{
    class Program
    {
        int FibonacciSequence(int item)
        {
            if (item == 0)
                return 0;
            else if (item == 1)
                return 1;

            return FibonacciSequence(item - 1) + FibonacciSequence(item - 2);
        }

        static void test1()
        {
            Program obj = new Program();
            Stopwatch watch = new Stopwatch();
            watch.Start();
            int nR = obj.FibonacciSequence(39);
            watch.Stop();
            Console.WriteLine(nR + "=>" + watch.ElapsedMilliseconds);
        }

        void matrixMultiplication(int count, int[][] ppA, int[][] ppB,ref int[][] ppR)
        {
            for (int row = 0; row < count; row++)
            {
                for (int col = 0; col < count; col++)
                {
                    ppR[row][col] = 0;
                    for (int k = 0; k < count; k++)
                    {
                        ppR[row][col] += ppA[row][col] * ppB[col][row];
                    }
                }
            }
        }

        static void test2()
        {
            int count = 500;
            int[,] a = new int[count, count];
            int[,] b = new int[count, count];
            int[,] c = new int[count, count];

            Program obj = new Program();
            Stopwatch watch = new Stopwatch();
            watch.Start();
            matrixMultiplication(count, a, b, ref c);

            watch.Stop();
            Console.WriteLine("=>" + watch.ElapsedMilliseconds);
        }

        static void Main(string[] args)
        {
            //for(int i=0;i<3;i++)
            //{
            //    test1();
            //}
            for (int i = 0; i < 3; i++)
            {
                test2();
            }
        }
    }
}



java源代码
public class FibonacciSequence {
    public int _FibonacciSequence(final int item)
    {
        if (item == 0)
            return 0;
        else if (item == 1)
            return 1;

        return _FibonacciSequence(item - 1) + _FibonacciSequence(item - 2);
    }
    
    static void test1()
    {
		long start = System.currentTimeMillis();
		FibonacciSequence obj = new FibonacciSequence();
		int nR = obj._FibonacciSequence(39);
		long stop = System.currentTimeMillis();
		System.out.println(Integer.toString(nR)+"=>" + Integer.toString((int) (stop-start)));	
    }
    
    void matrixMultiplication(int count, int[][] ppA, int[][] ppB,int[][] ppR)
    {
        for (int row = 0; row < count; row++)
        {
            for (int col = 0; col < count; col++)
            {
                ppR[row][col] = 0;
                for (int k = 0; k < count; k++)
                {
                    ppR[row][col] += ppA[row][col] * ppB[col][row];
                }
            }
        }
    }

    static void test2()
    {		
    	int count=500;
    	int[][] ppA = new int[count][count];
    	int[][] ppB = new int[count][count];
    	int[][] ppR = new int[count][count];
    	
		long start = System.currentTimeMillis();
		FibonacciSequence obj = new FibonacciSequence();	

		
		obj.matrixMultiplication(count,ppA,ppB,ppR);
		
		long stop = System.currentTimeMillis();
		System.out.println("=>" + Integer.toString((int) (stop-start)));	
    }

    public static void main(String args[])
	{
//    	for(int i=0;i<3;i++)    	
//    		test1();
    	for(int i=0;i<3;i++)
    		test2();
	}
}



JavaScript源代码

<html>
	<head>
		<script type="text/javascript">
			function prepareData(count)
			{
				var a = new Array();
				for(var k=0;k<count;k++){
					 a[k]=new Array();
					 for(var i=0;i<count;i++)
					 	a[k][i]=i;
				}
				return a;
			}
			
			function  FibonacciSequence(item)
      {
	       if (item == 0)
	       		return 0;
	       else if (item == 1)
	       	return 1;
	       	
	       return FibonacciSequence(item - 1) + FibonacciSequence(item - 2);
	    }
	    
	    function test1()
	    {
				var startTime = new Date().getTime(); 
				var nR = FibonacciSequence(39);
				var stopTime = new Date().getTime(); 
				var nElapsed = stopTime-startTime;
				console.log("FibonacciSequence="+nR+"=>"+nElapsed);
	    }
	    
	    function matrixMultiplication(a,b){
	    	var len=a.length,arr=[];
	    	for(var i=0;i<len;i++){
	    		arr[i]=[];
	    	  
	    	  for(var j=0;j<len;j++){
	    		  arr[i][j]=0;
	    	    for(var k=0;k<len;k++)
	    	    {
	    		    arr[i][j]+=a[i][k]*b[k][j];//
	      	  }
	       	}
        }
      return arr;
      }
	    
	    /*	    
	    for(var i=0;i<3;i++)
	    	test1();
	    */	
	    var a = prepareData(500);
	    var b = prepareData(500);
	    
	    for(var i=0;i<3;i++)
	    {
	    	var startTime = new Date().getTime(); 
	    	matrixMultiplication(a,b);	    	
	    	var stopTime = new Date().getTime(); 
				var nElapsed = stopTime-startTime;
				console.log("matrixMultiplication=>"+nElapsed);
	    }
		</script>
	</head>
	<body>
	</body>
</html>


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