Java性能评估 [英] Java Performance measurement

查看:108
本文介绍了Java性能评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在类之间进行一些Java性能比较,想知道是否存在某种Java Performance Framework可以简化编写性能测量代码的过程?

I am doing some Java performance comparison between my classes, and wondering if there is some sort of Java Performance Framework to make writing performance measurement code easier?

即,我现在正在尝试衡量与使用AtomicInteger作为我的同步器"相比,具有PseudoRandomUsingSynch.nextInt()中的同步"方法具有什么效果.

I.e, what I am doing now is trying to measure what effect does it have having a method as "synchronized" as in PseudoRandomUsingSynch.nextInt() compared to using an AtomicInteger as my "synchronizer".

因此,我尝试使用3个线程访问10000次循环访问同步方法来测量生成随机整数所需的时间.

So I am trying to measure how long it takes to generate random integers using 3 threads accessing a synchronized method looping for say 10000 times.

我确信有更好的方法可以做到这一点.你能开导我吗? :)

I am sure there is a much better way doing this. Can you please enlighten me? :)

public static void main( String [] args ) throws InterruptedException, ExecutionException {
    PseudoRandomUsingSynch rand1 = new PseudoRandomUsingSynch((int)System.currentTimeMillis());
    int n = 3;
    ExecutorService execService = Executors.newFixedThreadPool(n);

    long timeBefore = System.currentTimeMillis();
    for(int idx=0; idx<100000; ++idx) {
        Future<Integer> future = execService.submit(rand1);
        Future<Integer> future1 = execService.submit(rand1);
        Future<Integer> future2 = execService.submit(rand1);

        int random1 = future.get();
        int random2 = future1.get();
        int random3 = future2.get();

    }
    long timeAfter = System.currentTimeMillis();
    long elapsed = timeAfter - timeBefore;
    out.println("elapsed:" + elapsed);
}

班级

public class PseudoRandomUsingSynch implements Callable<Integer> {
private int seed;

public PseudoRandomUsingSynch(int s) { seed = s; }

public synchronized int nextInt(int n) {
    byte [] s = DonsUtil.intToByteArray(seed);
    SecureRandom secureRandom = new SecureRandom(s);
    return ( secureRandom.nextInt() % n );
}

@Override
public Integer call() throws Exception {
    return nextInt((int)System.currentTimeMillis());
}
}

致谢

推荐答案

忽略微基准测试在您的情况下是否有用的问题(斯蒂芬C的观点非常有效),我会指出退出:

Ignoring the question of whether a microbenchmark is useful in your case (Stephen C' s points are very valid), I would point out:

首先,不要听别人说没那么难"的人.是的,使用JIT编译在虚拟机上进行微基准测试很困难.从微基准测试中获取有意义和有用的数据实际上真的很困难,任何声称不难的人要么是天才,要么就是做错了. :)

Firstly, don't listen to people who say 'it's not that hard'. Yes, microbenchmarking on a virtual machine with JIT compilation is difficult. It's actually really difficult to get meaningful and useful figures out of a microbenchmark, and anyone who claims it's not hard is either a supergenius or doing it wrong. :)

第二,是的,周围有一些这样的框架.值得一看的(认为它处于发布前的很早的阶段)是Kevin撰写的 Caliper Google的Bourrillion和Jesse Wilson.从一些早期的观察来看,看起来确实令人印象深刻.

Secondly, yes, there are a few such frameworks around. One worth looking at (thought it's in very early pre-release stage) is Caliper, by Kevin Bourrillion and Jesse Wilson of Google. Looks really impressive from a few early looks at it.

这篇关于Java性能评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆