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!
Yes, readability. The first is far more clear about the intent.
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.