Java和C#运行速度对比:Java比C#快约3倍

测试条件:

Java版本: Java 8,

.NET版本:v4.0, Release模式,针对x86平台优化

单线程模式。

 

测试1:(测试1的代码摘自http://blog.csdn.net/houjin_cn/article/details/5957432

Java代码:

 

package javatest;

public class temp
{
    public static void main(String[] args)
    {
        long nn = System.nanoTime();
        int times = 9999;
        factorial(times);
        nn = System.nanoTime() - nn;// 单位为ns(纳秒,即10的负9次方秒),
        System.out.println((nn / 1000000) + " ms");
    }

    private final static void factorial(int n)
    {
        int[] bs = new int[16384];
        int top = 1;
        bs[0] = 1;
        for (int i = 2; i <= n; i++)
        {
            int w = 0;
            for (int ic = 0; ic < top; ic++)
            {
                int tn = bs[ic];
                tn = tn * i + w;
                w = tn >>> 16;
                bs[ic] = tn & 0xffff;
            }
            if (w != 0)
            {
                bs[top++] = w;
            }
        }
    }
}

 

 

C#代码:

 

static void Main(string[] args)
{
   long nn = System.DateTime.Now.Ticks;
   factorial(9999);
   nn = System.DateTime.Now.Ticks - nn;// 单位为刻度数,1刻度数=10的负7次方秒
   System.Console.Out.Write((nn / 10000) + " ms");
   System.Console.Out.WriteLine();
   Console.Read();
}

static void factorial(int n)
{
 int[] bs = new int[16384];
 int top = 1;
 bs[0] = 1;
 for (int i = 2; i <= n; i++)
 {
   int w = 0;
   for (int ic = 0; ic < top; ic++)
   {
     int tn = bs[ic];
     tn = tn * i + w;
     w = tn >> 16;
     bs[ic] = tn & 0xffff;
   }
   if (w != 0)
   {
     bs[top++] = w;
   }
 }
}

 

 

 

测试结果

Java:

C#:



结论1Java速度是C#2.8

 

 

测试2:同一个函数,执行两亿次。

Java代码(省略了打印):

package javatest;

import java.text.DecimalFormat;
import java.util.Date;

public class speedTest
{
    public static void main(String[] args)
    {
        int i = 0;
        int max = 200000000;//两亿次
        Date start = new Date();
        while (i++ < max)
        {
            transform();
        }
        Date end = new Date();
        long diff = end.getTime() - start.getTime();
        
        StringBuffer maxToshow=new StringBuffer();
        maxToshow.append(max);
        int count=0;
        for(int a=1;a<String.valueOf(max).length();a++)
        {
            if(a%3==0)
            {
                count++;
                maxToshow.insert(String.valueOf(max).length()-count*3, ",");
            }
        }
        //String s = String.format("it takes %f seconds to run %s loops",
        //        diff / 1000.00, new DecimalFormat(",###").format(max));
        String s = String.format("it takes %f seconds to run %s loops",
                diff / 1000.00, maxToshow.toString());
        System.out.println(s);
    }

    public static void transform()
    {
        String str = "Pcybgle rfgq rsrmpgyj fyq npmzyzjw pcgldmpacb wmsp glrcpcqr gl sqgle Nwrfml - wms qfmsjb zc cyecp rm ynnjw Nwrfml rm qmjtgle wmsp pcyj-umpjb npmzjckq. Ufcpc qfmsjb wms em rm jcypl kmpc?";
        for (int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            if (c >= 'a' && c <= 'z')
            {
                c += 2;
                if (c > 'z')
                {
                    c -= 26;
                }
                // System.out.print(c);
            }

            else if (c >= 'A' && c <= 'Z')
            {
                c += 2;
                if (c > 'Z')
                {
                    c -= 26;
                }
                // System.out.print(c);
            }
            else
                ;// System.out.print(c);
        }
    }
}

 

C#代码(省略了打印)

 

static void Main(string[] args)
    {
        int i = 0;
        int max = 200000000;// 两亿次
        DateTime start = DateTime.Now;
        while (i++ < max)
        {
            transform();
        }
        Console.WriteLine();
        DateTime end = DateTime.Now;
        Console.WriteLine("it takes {0} seconds to run {1} loops",
                (end - start).TotalSeconds, max);
        Console.Read();
    }

    public static void transform()
    {
        String str = "Pcybgle rfgq rsrmpgyj fyq npmzyzjw pcgldmpacb wmsp glrcpcqr gl sqgle Nwrfml - wms qfmsjb zc cyecp rm ynnjw Nwrfml rm qmjtgle wmsp pcyj-umpjb npmzjckq. Ufcpc qfmsjb wms em rm jcypl kmpc?";
        for (int i = 0; i < str.Length; i++)
        {
            int c = str[i];
            if (c >= 'a' && c <= 'z')
            {
                c += 2;
                if (c > 'z')
                {
                    c -= 26;
                }
                // Console.Write((char)c);
            }
            else if (c >= 'A' && c <= 'Z')
            {
                c += 2;
                if (c > 'Z')
                {
                    c -= 26;
                }
                // Console.Write((char)c);
            }
            else
                ;// Console.Write((char)c);
        }
    }

 

 

 

 

 

 

测试结果:

Java:

C#:

 

结论2Java速度是C#3.74倍:

posted @ 2016-01-19 15:13  morein2008  阅读(7120)  评论(0编辑  收藏  举报