3 ms·
From my experience, what makes program slow more often have to do with memory than CPU cycles. More memory use mean more cache misses, and cache misses have a
by GuB-42 8y ago
From my experience, what makes program slow more often have to do with memory than CPU cycles.
More memory use mean more cache misses, and cache misses have a huge impact. Roughly, L2 cache is 10x slower than L1 and RAM is 10x slower than L2.
With C, when you want to allocate some memory, you typically precalculate the size and malloc() just the right amount. In C++, you are more likely to use containers, which are safer and more flexible but tend to introduce some overhead. GC languages like Java have the GC overhead in addition and dynamic languages like Python are the worst because they also need to keep track of object types.
Now there are some applications that are really CPU-limited, like complex calculations. But in that case, chances are that you'll probably want to offload things to the GPU.
Still, I really liked the article. That's another great tool to have when it comes to performance. But it won't change the general case where C > C++ > Java > Python when it comes to performance.
A little caveat when it comes to C vs C++ though. C++ can be faster than C if you use C-like memory management, but that's not the usually recommended way of doing things.