这是否意味着Java Math.floor非常慢? [英] Does this mean that Java Math.floor is extremely slow?

查看:141
本文介绍了这是否意味着Java Math.floor非常慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java不多。

我正在编写一些优化的数学代码,我对我的分析器结果感到震惊。我的代码收集值,交错数据,然后根据它选择值。 Java运行速度比我的C ++和MATLAB实现慢。

I am writing some optimized math code and I was shocked by my profiler results. My code collects values, interleaves the data and then chooses the values based on that. Java runs slower than my C++ and MATLAB implementations.

我正在使用 javac 1.7.0_05
我使用的是Sun / Oracle JDK 1.7.05

I am using javac 1.7.0_05 I am using the Sun/Oracle JDK 1.7.05

存在一个在代码中执行相关任务的floor函数。

There exists a floor function that performs a relevant task in the code.


  1. 有人知道解决此问题的范式方法吗?

  2. 我注意到我的 floor()函数定义为名为 StrictMath 的东西。是否有类似 -ffast-math 的Java?我期待必须有一种方法可以将地板功能更改为计算上更合理的东西,而无需自己编写。

  1. Does anybody know of the paradigmatic way to fix this?
  2. I noticed that my floor() function is defined with something called StrictMath. Is there something like -ffast-math for Java? I am expecting there must be a way to change the floor function to something more computationally reasonable without writing my own.

public static double floor(double a) {
    return StrictMath.floor(a); // default impl. delegates to StrictMath
}


编辑

所以有些人建议我尝试演员。我尝试了这一点,并且壁挂时间绝对没有变化。

So a few people suggested I try to do a cast. I tried this and there was absolutely no change in walltime.

private static int flur(float dF)
{
    return (int) dF;
}

413742投注功能

413742 cast floor function

394675 Math.floor

394675 Math.floor

这些测试是在没有探查器的情况下运行的。我们努力使用一个分析器,但运行时间发生了翻天覆地的变化(15分钟以上我退出)。

These test were ran without the profiler. An effort was made to use a profiler but the runtime was drastically altered (15+ minutes so I quit).

推荐答案

这是一个完整性检查您的假设,即代码实际上花费了99%的时间在 floor 。假设您拥有算法的Java和C ++版本,这些版本在它们产生的输出方面都是正确的。为了论证,让我们假设两个版本调用相同的 floor 函数的次数相同。所以时间函数是

Here's a sanity check for your hypothesis that the code is really spending 99% of its time in floor. Let's assume that you have Java and C++ versions of the algorithm that are both correct in terms of the outputs they produce. For the sake of the argument, let us assume that the two versions call the equivalent floor functions the same number of times. So a time function is

t(input) = nosFloorCalls(input) * floorTime + otherTime(input)

其中 floorTime 是拨打<$ c的时间平台上的$ c> floor 。

现在,如果您的假设是正确的,并且 floorTime 在Java上的成本要高得多(大约需要大约99%的执行时间),那么你会期望Java版本的应用程序运行速度比C ++版本慢很多(50倍或更多)。如果你没有看到这个,那么你的假设很可能是假的。

Now if your hypothesis is correct, and floorTime is vastly more expensive on Java (to the extent that it takes roughly 99% of the execution time) then you would expect the Java version of the application to run a large factor (50 times or more) slower than the C++ version. If you don't see this, then your hypothesis most likely is false.

如果假设是假的,这里是对分析结果的两种替代解释。

If the hypothesis is false, here are two alternative explanations for the profiling results.


  1. 这是测量异常;即分析者以某种方式弄错了。尝试使用不同的分析器。

  1. This is a measurement anomaly; i.e. the profiler has somehow got it wrong. Try using a different profiler.

您的代码的Java版本中存在一个错误,导致它调用 floor 比C ++版本的代码多很多倍。

There is a bug in the Java version of your code that is causing it to call floor many, many more times than in the C++ version of the code.

这篇关于这是否意味着Java Math.floor非常慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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