我发现并用我的应用程序对其进行了测试。我正在调用和调用相同的方法。
我尝试了不同的连续执行以及不同的方法(加密、排序等),得到了以下结果。
Executions|Invoke-Call ratio
1 | 62,78%
10 | 107,58%
100 | 76,74%
1000 | 80,01%
10000 | 116,88%
100000 | 82,80%
1000000 | 91,67%
我检查了它是否可能是使用多个线程,但这不是我能说的。可能是什么解释?
为了进一步澄清我的基准的摘录:调用部分:
Executable executable = new Executable();
Method execute = executable.getClass().getMethod("execute");
System.out.println("# Startup finished.");
for (float i = 0; i <= 6; i++)
{
int executions = (int) Math.pow(10, i);
long start = System.nanoTime();
for (int j = 0; j <= executions - 1; j++)
{
execute.invoke(executable);
}
long stop = System.nanoTime();
long delta = stop - start;
System.out.println("Invoke;" + executions + ";" + delta);
}
System.out.println("# Shutdown finished.");
调用部分:
Executable executable = new Executable();
System.out.println("# Startup finished.");
for (float i = 0; i <= 6; i++)
{
int executions = (int) Math.pow(10, i);
long start = System.nanoTime();
for (int j = 0; j <= executions - 1; j++)
{
executable.execute();
}
long stop = System.nanoTime();
long delta = stop - start;
System.out.println("Invoke;" + executions + ";" + delta);
}
System.out.println("# Shutdown finished.");
对于这个 Executable 类的例子,我特别注意从执行方法中排除所有准备工作。
public class Executable
{
private int index = 0;
private int testsize = 1111111;
private byte[][] plain = new byte[testsize][];
private byte[][] hashed = new byte[testsize][];
private SecureRandom securerandom;
private MessageDigest messagedigest;
public Executable()
{
this.securerandom = new SecureRandom();
this.messagedigest = MessageDigest.getInstance("SHA-256");
for (int i = 0; i <= testsize - 1; i++)
{
this.plain[i] = new byte[8];
this.securerandom.nextBytes(this.plain[i]);
}
}
public void execute()
{
messagedigest.update(this.plain[index]);
this.hashed[index] = messagedigest.digest();
index++;
}
}