4 ms·
On my random forays into C++, I saw different things. Here were my three most important optimizations. 1. Replace strings with integers wherever possible. Cr
by btilly 13y ago
On my random forays into C++, I saw different things. Here were my three most important optimizations.
1. Replace strings with integers wherever possible. Create a mapping table, use the indexes.
2. Layout related objects close in memory. Even if it requires special data structures to do. Suppose that if I need A I am likely to need B soon. If B is physically close to A, then I am likely to find it in cache, which is about 10x as fast. So, for instance, don't store a tree as a bunch of random pointers. Store it as offsets inside of a fixed container of memory.
3. Process data according to how it is laid out in memory. For instance walk through a vector, don't do random access into it. Again this is all about trying to make sure that stuff is in cache as often as possible.
For the second and third points I found it was a case of do it perfectly or don't worry about it. For example if I am missing cache regularly in three places, fixing one only mades my code 50% faster. Fixing the second one almost doubled my speed. Getting the last one was an order of magnitude speed increase.
- TillE 13y agoAfter making sure you're using the correct basic algorithms, cache optimization is probably the one major thing that's really worth thinking about. Even with hyperthreading and all the other fancy tricks that modern CPUs have, it's still a huge deal. I learned about all this stuff in general terms at university years ago, but it didn't really click until recently that even when the CPU is at 100%, it's very very often stuck waiting for a memory read to complete.
- a8da6b0c91d 13y ago> Replace strings with integers wherever possible If you're going to be doing a lot of comparisons of identical strings, use boost::flyweight<std::string>.