开发者

Floating-point data type for highest performance in J2ME

开发者 https://www.devze.com 2023-01-25 04:55 出处:网络
I want to use floating-point numbers in my J2ME (MIDP 2.0) program. I don\'t need high precision, so I\'m considering choosing float type.

I want to use floating-point numbers in my J2ME (MIDP 2.0) program.

I don't need high precision, so I'm considering choosing float type.

However, it seems to me that I read a Java article which says that double is better supported by processors, so it is faster.

I'm doing many operations with such numbers, so performance is important to me.

The question is, which one of these data types works faster开发者_Go百科 in J2ME?


The performance difference between float and double is not that significant.

If you are doing those operation very frequently than float is better also it consume less space.

Update:

 public long sumDouble(){

        double[] arr = new double[1000];
        for(int i = 0 ;i < 1000;i++){
            arr[i]=new Random().nextFloat();
        }
        float sum = 0.0f;
        long start = System.currentTimeMillis();
        for(int i = 0 ;i < 1000;i++){
            sum+=arr[i];
        }
        System.out.println(""+sum);
        long end = System.currentTimeMillis();
        return end - start;
    }
    public long sumFloat(){

        double[] arr = new double[1000];
        for(int i = 0 ;i < 1000;i++){
            arr[i]=new Random().nextDouble();
        }
        double sum = 0.0f;
        long start = System.currentTimeMillis();
        for(int i = 0 ;i < 1000;i++){
            sum+=arr[i];
        }
        System.out.println(""+sum);
        long end = System.currentTimeMillis();
        return end - start;
    }

Try executing these methods in your real device to measure the performance difference Its not much significant.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号