English 中文(简体)
量度双对大进度
原标题:Measure double vs BigDecimal

I wrote simple benchmark that test performance of multyplying doubles vs BigDecimal. Is my method correct? I use randomized values because compiler optimized multyplying constants many times (eg Math.PI * Math.E).
But:
- I don t know if generating random numbers inside a test corrupts the result.
- The same for creating new BigDecimal objects inside a test.

我想只测试乘法的性能(而不是建造者使用的时间)。

如何做到这一点?

import java.math.*;
import java.util.*;

public class DoubleVsBigDecimal
{
    public static void main(String[] args)
    {
        Random rnd = new Random();
        long t1, t2, t3;
        double t;

        t1 = System.nanoTime();

        for(int i=0; i<1000000; i++)
        {
            double d1 = rnd.nextDouble();
            double d2 = rnd.nextDouble();
            t = d1 * d2;
        }

        t2 = System.nanoTime();

        for(int i=0; i<1000000; i++)
        {
            BigDecimal bd1 = BigDecimal.valueOf(rnd.nextDouble());
            BigDecimal bd2 = BigDecimal.valueOf(rnd.nextDouble());
            bd1.multiply(bd2);
        }

        t3 = System.nanoTime();

        System.out.println(String.format("%f",(t2-t1)/1e9));
        System.out.println(String.format("%f",(t3-t2)/1e9));
        System.out.println(String.format("%f",(double)(t3-t2)/(double)(t2-t1)));
    }
}
最佳回答

您不仅在计时乘数操作, 还在计时其它事项 。

你需要做一些事情,比如:

    long time = 0; 
    for(int i=0; i<1000000; i++) {
        double d1 = rnd.nextDouble();
        double d2 = rnd.nextDouble();
        long start = System.nanoTime();
        t = d1 * d2;
        long end = System.nanoTime();
        time += (end-start)
    }
    long meantime = time / 1000000;

然后可能计算标准错误。 另外, 您可能需要在开始之前先用一些计算来暖和 jvm, 否则您在开始时会得到一些高值 。

问题回答

在 Java 中进行基准测试非常奇怪。 < / em> 例如, JVM 将无法真正完全优化一个代码, 直到它已经运行过很多次 -- 但是在优化后进行测量更公平, 因为任何生产系统都会多次使用这种方法 。

还有其他一系列带有 Java 基准的加查。 最简单的避免办法可能是使用专家所建的 Java 基准工具, 例如 : < a href=" http:// caliper.googlecode. com" rel = "nofollow" > Caliper < / a > 。

您可以先先生成两个收藏的 1000 双倍/ 双倍/ 双十进制, 并在两个嵌入循环中每个相乘 :

public static void main(String[] args)
{
    Random rnd = new Random();
    List <Double> dl = new ArrayList <Double> ();
    List <BigDecimal> bdl = new ArrayList <BigDecimal> ();

    for(int i=0; i<1000; i++)
    {
        double d = rnd.nextDouble();
        dl.add (d);
        bdl.add (new BigDecimal (d));
    }

    long t1 = System.nanoTime();
    double t;
    for (double d1 : dl)
            for (double d2 : dl)
                t = d1 * d2;

    long t2 = System.nanoTime();

    for (BigDecimal b1 : bdl)
        for (BigDecimal b2 : bdl)
                b1.multiply (b2);

    long t3 = System.nanoTime();

    System.out.println (String.format ("%f", (t2 - t1) / 1e9));
    System.out.println (String.format ("%f", (t3 - t2) / 1e9));
    System.out.println (String.format ("%f", (double) (t3 - t2) / (double) (t2 - t1)));
} 

第一种代码产生非常稳定的数值, 当重复时,

0,186755
10,970243
58,741445

我的代码是不同的价值,但也稳定下来:

0,077177
1,112490
14,414710

关系上的差别是1: 4, 几乎是一比4。 关系不是那么大, 比关系大十进制: 双倍,但是...

(i386-32,客户模式,JRE-1.6,灵异,甲骨文,2Ghz Centrino单核)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签