Fork me on GitHub

浮点运算在三种语言里的效率

今天闲来无事,突然想测试一下C#,C++,Java这三种我比较常用的语言在浮点数运算方面的效率,大家可以先不急往下看,猜想一下结果如何。
 
首先是算法,采用计算一个100*100的Mandelbrot set,连续调用1000次来进行计时,算法来自于:http://www.nicholson.com/rhn/jsbench2.html,一个Javascript引擎的Benchmark(这里再表扬下V8,不愧是最快的JS Engine,跑这个Benchmark,1.023秒,IE10还是有长进,1.163秒,差距不大,但是IE9,9秒......)。
 
OK,回归到正题,下面是测试所用的代码:
先是王者C语言:
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int qset(float xx,float yy,float u,float v){
	int n;
	float t,xsqr,ysqr;
	int lim = 100;
	float x = xx;
	float y = yy;
	xsqr = x*x;
	ysqr = y*y;
	for(n = 0;(n < lim) && (xsqr + ysqr < 4.0);n++){
		t = xsqr - ysqr + u;
		y = 2.0 * x * y +v;
		x = t;
		xsqr = t * t;
		ysqr = y * y;
	}
	return n;
}

int mb100(){
	int dots = 0;
	int res = 100;
	float a1 = -2.50;
	float b1 = -1.75;
	float s = 3.05;
	float x = 0;
	float y = 0;
	float g = s / res;
	int i,j,k;
	float a,b;
	
	for(j = 0,b = b1;j <res; b += g,j++){
		for(i = 0,a = a1;i < res;a += g,i++){
			k = qset(x,y,a,b);
			if(k > 90) {
				dots++;	
			}
		}
	}
	return dots;
}

int main(int argc, char **argv)
{
	clock_t start,finish;
	
	start=clock();
	for(int i=0;i<1000;i++){
		mb100();
	}
	finish=clock();
	
	long duration=finish-start;
	printf("%ld",duration);//1810 ms

	getchar();
	return 0;
}

再来是最受欢迎的JAVA:

public class Main {
    public static void main(String args[]) throws  Exception{
        long start=System.currentTimeMillis();
        for(int i=0;i<1000;i++){
            mb100();
        }
        long finish=System.currentTimeMillis();
        System.out.println(finish-start); //865ms
    }

    static int qset(float xx, float yy, float u, float v){
        int n;
        float t, xsqr, ysqr;
        int lim = 100;
        float x = xx;
        float y = yy;
        xsqr = x * x;
        ysqr = y * y;
        for (n = 0; (n < lim) && (xsqr + ysqr < 4.0); n++)
        {
            t = xsqr - ysqr + u;
            y = 2.0f * x * y + v;
            x = t;
            xsqr = t * t;
            ysqr = y * y;
        }
        return n;
    }

    static int mb100(){
        int dots = 0;
        int res = 100;
        float a1 = -2.50f;
        float b1 = -1.75f;
        float s = 3.05f;
        float x = 0;
        float y = 0;
        float g = s / res;
        int i, j, k;
        float a, b;

        for (j = 0, b = b1; j < res; b += g, j++)
        {
            for (i = 0, a = a1; i < res; a += g, i++)
            {
                k = qset(x, y, a, b);
                if (k > 90)
                {
                    dots++;
                }
            }
        }
        return dots;
    }
}

最后是开发效率最高的C#

#region 引用

using System;
using System.Diagnostics;

#endregion

namespace MBTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < 1000; i++)
            {
                MB100();
            }
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);//1810 ms
            Console.ReadKey();
        }

        private static int QSet(float xx, float yy, float u, float v)
        {
            int n;
            float t, xsqr, ysqr;
            int lim = 100;
            float x = xx;
            float y = yy;
            xsqr = x*x;
            ysqr = y*y;
            for (n = 0; (n < lim) && (xsqr + ysqr < 4.0); n++)
            {
                t = xsqr - ysqr + u;
                y = 2.0f*x*y + v;
                x = t;
                xsqr = t*t;
                ysqr = y*y;
            }
            return n;
        }

        private static int MB100()
        {
            int dots = 0;
            int res = 100;
            float a1 = -2.50f;
            float b1 = -1.75f;
            float s = 3.05f;
            float x = 0;
            float y = 0;
            float g = s/res;
            int i, j, k;
            float a, b;

            for (j = 0, b = b1; j < res; b += g, j++)
            {
                for (i = 0, a = a1; i < res; a += g, i++)
                {
                    k = QSet(x, y, a, b);
                    if (k > 90)
                    {
                        dots++;
                    }
                }
            }
            return dots;
        }
    }
}

输出的结果已经注释在代码中了,我没想到的是JAVA居然是最快的,而且还不是快了一星半点儿,只需要C/C#的50%左右,难道是JRE对运算专门做了优化?之前还用过计算SHA-512来进行比较,依然是JAVA最快,不知道是JRE优化还是编译器优化了,有高手能解释下么。不过就C和C#来说,运算方面,只要C没有做过特别优化,效率相差并不大(几乎相同),但是C语言这种只有高手没有菜鸟的语言,要想优化的更好,确实比较困难。

 

posted @ 2013-06-24 18:09  sweetwxh  阅读(3050)  评论(3编辑  收藏  举报