by jokoon · 12y ago · 0 comments
I already read the classical argument of having data oriented designs and to avoid linked lists, but I've never really heard of anything concrete on performance in programming.
All I hear is "profile profile profile". I guess most of the time it will point out a place where my code is slow, but if I find one, how do I solve it ?
For example, can I create some simple bit of code that do a cache miss at will ? Any example of such code ? How do I detect a cache miss ?
To create a cache miss in one processor all you need to force the processor to look at M different memory addresses that map to the same cache location, where M is higher than the N-way associativity for your processor. The specifics of this differ for each processor, but as an example, Intel Sandy Bridge CPUs have 8-way associativity, so if you fetch 9 addresses that are 4096 bytes apart you will cause a cache miss.
Lots of small tips and links if you Google, e.g.
http://stackoverflow.com/questions/8744088/what-is-the-best-...
http://stackoverflow.com/questions/3359524/detecting-cache-m...
The full detail is in books like http://www.intel.co.uk/content/www/uk/en/architecture-and-te...
So, "profile, profile, profile", but also understand factors like this that pull in different directions in different scenarios.
Detecting cache misses can be done with valgrind: http://valgrind.org/docs/manual/cg-manual.html
There are architecture-specific instruction-level profilers as well. This video mentions a couple and is also a great overview of how non-intuitive performance can be with modern CPUs: http://channel9.msdn.com/Events/Build/2014/4-587
In this case it is useful to look at "Cache-oblivious algorithms": Cache oblivious algorithms allow you to write code that performs well regardless of the architecture and processor model they are run on. From Wikipedia: «a cache-oblivious algorithm (or cache-transcendent algorithm) is an algorithm designed to take advantage of a CPU cache without having the size of the cache (or the length of the cache lines, etc.) as an explicit parameter.» http://en.wikipedia.org/wiki/Cache-oblivious_algorithm
Sadly the field is new and there are not many such algorithms around, but you can already find algorithms for many common tasks such as traversing linked lists and calculating FFTs.
This is a good intro video on the subject: https://www.youtube.com/watch?v=16ZF9XqkfRY
http://tacc-web.austin.utexas.edu/veijkhout/public_html/istc...