程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Java性能评估大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Java性能评估?

开发过程中遇到Java性能评估的问题如何解决?下面主要结合日常开发的经验,给出你关于Java性能评估的解决方法建议,希望对你解决Java性能评估有所启发或帮助;

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

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

其次,是的,周围有一些这样的框架。一个值得看的(认为它在非常早期的预发布阶段)的显卡,由凯文·Bourrillion和谷歌的杰西·威尔逊。从一些早期的观察来看,看起来确实令人印象深刻。

解决方法

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

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

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

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

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());
}
}

问候

大佬总结

以上是大佬教程为你收集整理的Java性能评估全部内容,希望文章能够帮你解决Java性能评估所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: