Algorithm speed with double and int?

canvas, double, gwt, integer, java

Solution

You're in GWT, so ultimately your code will be JavaScript, and JavaScript has a single type for numeric data: `Number`, which corresponds to Java's `Double`.

Using integers in GWT can either mean (I have no idea what the GWT compiler exactly does, it might also be dependent on context, such as crossing JSNI boundaries) that the generated code is doing more work than with doubles (doing a narrowing conversion of numbers to integer values), or that the code won't change at all.

All in all, expect the same or slightly better performance using doubles (unless you have to later do conversions to integers, of course); but generally speaking you're over-optimizing (also: optimization needs measurement/metrics; if you don't have them, you're on the “premature optimization” path)

Problem

how do algorithms with `double` compete compared to `int` values? Is there much difference, or is it neglectable? In my case, I have a canvas that uses `Integers` so far. But now as I'm implementing a scaling, I'm probably going to switch everything to `Double`. Would this have a big impact on calculations? If so, would maybe rounding doubles to only a few fractions optimize performance? Or am I totally on the path of over-optimization and should just use doubles without any headache?

Original source

Related problems