最有效的方式找到最​​小的3个数字的Java? [英] Most efficient way to find smallest of 3 numbers Java?

查看:179
本文介绍了最有效的方式找到最​​小的3个数字的Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java编写的算法,我想更有效。我认为可以更有效的一部分被发现的最小的3个数字。目前我使用了 Math.min 的方法如下:

I have an algorithm written in Java that I would like to make more efficient. A part that I think could be made more efficient is finding the smallest of 3 numbers. Currently I'm using the Math.min method as below:

double smallest = Math.min(a, Math.min(b, c));

如何有效的,这是?难道是更有效地替换,如果像下面的语句:

How efficient is this? Would it be more efficient to replace with if statements like below:

double smallest;
if(a<b && a<c){
    smallest = a;
}else if(b<c && b<a){
    smallest = b;
}else{
    smallest = c;
}

或者,如果任何其他的方法是更有效的

Or if any other way is more efficient

我不知道是否值得改变的是什么我目前使用?

I'm wondering if it is worth changing what I'm currently using?

任何速度的提升将是很大的帮助。

Any speed increase would be greatly helpful

推荐答案

没有,这是严重的没有的价值变化。改进的排序,你打算用微的优化这样的摆弄不会是值得的时候得到的。即使函数被调用足够的方法调用的成本将被删除。

No, it's seriously not worth changing. The sort of improvements you're going to get when fiddling with micro-optimisations like this will not be worth it. Even the method call cost will be removed if the min function is called enough.

如果你有你的算法有问题,最好的办法是寻找到宏观的优化(大画面的东西,如算法选择或调整) - 你通常得到的的更好的性能改善那里。

If you have a problem with your algorithm, your best bet is to look into macro-optimisations ("big picture" stuff like algorithm selection or tuning) - you'll generally get much better performance improvements there.

和您的意见,取消 Math.pow 给改进很可能是正确的,但,这是因为它是一个相对昂贵的操作。 Math.min 甚至不会接近,在成本方面。

And your comment that removing Math.pow gave improvements may well be correct but that's because it's a relatively expensive operation. Math.min will not even be close to that in terms of cost.

这篇关于最有效的方式找到最​​小的3个数字的Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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