You mean "sum * Math.log(sum)"?
That's nice, I'll go with that.

javac definitely isn't allowed to do that kind of transformation -- it
actually can't do much of anything.
ProGuard might -- it's actually a dynamite byte code optimizer and
I've been itching to get it re-integrated into the build for stuff
like this. The JIT probably wouldn't do this either as I doubt it can
prove no side effects due to the native method call.

So yeah it's still worthwhile to be prudent when programming for speed
in Java as not much is going to get done later for you.


On Thu, Apr 29, 2010 at 6:45 PM, Ted Dunning <ted.dunn...@gmail.com> wrote:
> I think a cap is a good thing if the error is relatively small ( < 1e-4 or
> so).
>
> Betraying my age, I usually rewrite this as this:
>
>   for (int element : elements) {
>     if (element > 0) {
>       result += element * (Math.log(element));
>     }
>   }
>   result -= elements.size() * Math.log(sum)
>
> But presumably the compiler will notice this change and lift it out of the
> inner loop

Reply via email to