4 ms·
> its function calls are 30x faster (roughly, I measured it just now with a for loop doing 100 million function calls, which took 0.5s in JS, 15s in python) Yo
by ptx 4y ago
> its function calls are 30x faster (roughly, I measured it just now with a for loop doing 100 million function calls, which took 0.5s in JS, 15s in python)
Your benchmark also measures iterators and boxed integers, which were not in the JavaScript version, so it's not clear how much of the difference is due to function call overhead.
(Of course, it certainly doesn't help Python's performance that there's no simple way to do loops without iterators and integers without boxing. It would be nice if a future version could optimize the abstractions away.)
- SemanticStrengh 4y agoJS integers are value types unlike Java?!
- ptx 4y agoOn second thought, I'm not sure. Can you observe the number's object identity like you can in Python? Does JavaScript have to allocate a new Number object for each "i++"? My point was that the Python version of Aardwolf's function call benchmark [1] is definitely using iterators and boxing all numbers, so it's doing a lot more than just calling a function, and the Java equivalent would look something like this: Long f(Integer x) { return new Long(x.longValue() * x.longValue()); } // ... Double v = new Double(0.0); Iterator<Integer> range = IntStream.range(0, 100000000).iterator(); while (range.hasNext()) { Integer i = range.next(); v = new Double(v.doubleValue() + f(i).doubleValue()); } [1] https://news.ycombinator.com/item?id=31427506 https://news.ycombinator.com/item?id=31427506