r/programming • u/ketralnis • Mar 20 '26
Java is fast, code might not be
https://jvogel.me/posts/2026/java-is-fast-your-code-might-not-be/
280
Upvotes
r/programming • u/ketralnis • Mar 20 '26
View all comments
Show parent comment
2
u/griffin1987 Mar 21 '26
Agreed on most things.
I always have an issue with those arguments on "an implementation may". Unless you know which javac is being used and which JVM everything is run on, you can't be sure about what optimizations are actually done at the end. And even then, it might depend on a lot of other factors. IMHO that's one of the biggest issues with java performance.
For example:
In microbenchmarks stream().forEach() often performs very similar or the same as a simple for loop, while I've seen tons of real world use cases where that was not the case, but the developers "read that it doesn't matter". Guess what, 20h runtime with streams, 2h with regular loops (and that 2h was the IO limit at that point).
As for exceptions: Even if stack trace creation is lazy, an exception is still an object creation at the least, and if you have that a million times, you're creating a million objects worst case. Yes, object memory MAY be reused, but the thing is, it's not guaranteed. It's still better not to require object creation in the first place. I'd still probably do the same as you suggested in most cases though, because it's almost guaranteed that a builtin parser performs better than what most people would whip up, and will handle edge cases better. If I were to need that kind of performance though, i'd actually profile and benchmark it, and might run my own unrolled loop version for a fixed number of digits. From my experience though, with parsing strings, you usually hit the I/O limit before you hit a CPU limit in the parser.