My boss just told me that he learned about fast VB6 algorithms from a book and that the shortest way to write things is not necessarily the fastest (e.g. builtin methods are sometimes way slower than selfwritten ones because they do all kinds of checking or unicode conversions which might not be necessary in your case).
Now I wonder, is there a website with info on fast different constructs are in various languages, esp. Java/C#/Python/… (also C++ but there are so many compilers which probably differ a lot).
E.g. is there a difference between
if (a()) b();
and
a() && b();
Another example: is a = a * 4
maybe compiled to the same code as a <<= 2
?
I could test this myself, of course, writing both then running them 100000 times and comparing the runtime, but I'd also like to learn about new ways to write things, maybe even things that I hadn't considered before. Thanks for your answers!
Java uses objects, while C uses functions. Java is easier to learn and use because it's high level, while C can do more and perform faster because it's closer to machine code.
Elapsed Time. Based on these results, C is 2.34 times slower than Java and Python is 33.34 times slower than Java.
However, according to benchmarks around the web, C# tends to perform better overall. Its response time is shorter, besides taking up less CPU load. To note fairly, when coupled together with Just-In-Time compilers, Java provides high performance too.
Speed and performance Java is a favorite among developers, but because the code must first be interpreted during run-time, it's also slower. C++ is compiled to binaries, so it runs immediately and therefore faster than Java programs.
I would say these are likely to be the kind of micro-optimizations that wouldn't make a difference and aren't worth the effort.
Algorithm choice does matter, but the books you should be reading should be more like this or this.
If you really want to see whether the bit hacks you've cited make a difference, I'd recommend getting a performance baseline on the code you want to change first. Make your changes and remeasure performance the same way. If you get a result that says it's worth it, by all means continue.
You're better off profiling your code and finding out where the slowest part of your code lives and where the most work is being done. Guessing rarely works when optimizing.
is there a difference between
if (a()) b();
anda() && b();
?
Yes, readability. The first is far more clear about the intent.
Is
a = a * 4
maybe compiled to the same code asa <<= 2
?
Most likely yes. But even if they ended up as different CPU instructions the difference in time would be very small, and dependent on the instructions before and after.
Micro-Optimizing for modern CPU's is
In conclusion, write readable code first. When you do have a performance problem, profile and measure first.
As an Application Developer you should worry about using the right algorithms, like not reading a collection more than needed etc. But on the instruction/statement level, there are too many layers (C# Compiler, IL Compiler, Optimizers, pipelined CPU) between you and what actually gets executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With