English 中文(简体)
大整数中的乘法时间
原标题:Multiplication time in BigInteger

我的微型基准:

import java.math.*;
import java.util.*;
import java.io.*;
public class c
{
    static Random rnd = new Random();
    public static String addDigits(String a, int n)
    {
        if(a==null) return null;
        if(n<=0) return a;
        for(int i=0; i<n; i++)
            a+=rnd.nextInt(10);
        return a;
    }
    public static void main(String[] args) throws IOException
    {
        int n = 10000; \number of iterations
        int k = 10;    \number of digits added at each iteration

        BigInteger a;
        BigInteger b;

        String as = "";
        String bs = "";
        as += rnd.nextInt(9)+1;
        bs += rnd.nextInt(9)+1;
        a = new BigInteger(as);
        b = new BigInteger(bs);
        FileWriter fw = new FileWriter("c.txt");
        long t1 = System.nanoTime();
        a.multiply(b);
        long t2 = System.nanoTime();
        //fw.write("1,"+(t2-t1)+"
");
        if(k>0) {
            as = addDigits(as, k-1);
            bs = addDigits(as, k-1);
        }
        for(int i=0; i<n; i++)
        {
            a = new BigInteger(as);
            b = new BigInteger(bs);
            t1 = System.nanoTime();
            a.multiply(b);
            t2 = System.nanoTime();
            fw.write(((i+1)*k)+","+(t2-t1)+"
");
            if(i < n-1)
            {
                as = addDigits(as, k);
                bs = addDigits(as, k);
            }
            System.out.println((i+1)*k);
        }       

        fw.close();
    }
}

它测量正位数 BigIntug 的乘数乘数时间

Result: enter image description here

You can easily see the trend but why there is so big noise above 50000 digits? It is because of garbage collector or is there something else that affects my results? When performing the test, there were no other applications running.

仅使用奇数的测试结果。 测试时间较短( n=1000, k=100) 。

""https://i.sstatic.net/5uYuA.png" alt="此处输入图像描述"/ >

Odd digits (n=10000, k=10) enter image description here

如你所见,有巨大的噪音 在65000到70000之间,我想知道为什么...

Odd digits (n=10000, k=10), System.gc() every 1000 iterations enter image description here Results in noise between 50000-70000

最佳回答

我也怀疑这是JVM热身效应。 不是班级加载或 JIT 编译的热身, 而是堆积的热身。

将一个(java)环绕整个基准,然后运行几次。 (如果这给您提供了与以前相同的图表... 你会有“强势 > 证据 < / 强势”, 证明这不是一个热效果。 目前您没有任何经验证据 。)


另一种可能性是,噪音是由你的基准与操作系统 和(或)机器上的其他物体的相互作用引起的。

  • You are writing your timing data to an unbuffered stream. That means LOTS of syscalls, and (potentially) lots of fine-grained disc writes.
  • You are making LOTS of calls to nanoTime(), and that might introduce noise.
  • If something else is running on your machine (e.g. you are web browsing) that will slow down your benchmark for a bit and introduce noise.
  • There could be competition over physical memory ... if you ve got too much running on your machine for the amount of RAM.

最后,一定数量的噪音是不可避免的,因为每一个这些 multiply 电话都会产生垃圾,垃圾收集者将需要努力处理。


最后,如果你手动运行垃圾收集器(或增加堆积大小)以“平滑”数据点,你实际上正在做的是隐藏multiply 调用的成本之一。 所产生的图表看起来不错, 但具有误导性 :

  • The noisiness reflects what will happen in real life.
  • The true cost of the multiply actually includes the amortized cost of running the GC to deal with the garbage generated by the call.

要获得反映 BigInteger 在现实生活中行为方式的测量数据,您需要多次测试,计算平均时间,并将曲线与平均数据点相匹配。

记住,游戏的真正目的是 取得科学上有效的结果 而不是一个平稳的曲线

问题回答

如果您做了一个微基准, 您必须先“ 暖化” JVM, 才能让 JIT 优化代码, 然后可以测量性能 。 否则您将测量 JIT 完成的工作, 并在每次运行时改变结果 。

“噪音”发生的原因可能是 CPU的缓存被超过 并且性能开始降低





相关问题
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 ...

热门标签