How small should be an algorithm to be stored in cache? (Need a clue)
I'm doing a chess moves generator, I have the opportunity to replace 'while/for loops' with many 'if statements' and I was wondering 开发者_开发知识库if adding those ~3000 lines would improve performance as in theory or just make the algorithm be too big to be stored in cpu cache
I know it depends of the cache size (I got an AMD Phenom 8650 Triple Core 2.3) but I really don't have IDEA
First of all, you probably have a few MB of cache, so I doubt it would fill the entire cache.
Besides that, the CPU is busy doing lots of things beside your code, so I doubt the entire cache will be used just for your code.
Besides that, the movement between RAM (you probably got a few GB) to the cache is pretty neglectable.
So, yea, removing the loop (if it's a constant length loop) and replacing it with explicit lines should give you an improvement.
The amount or percentage of the improvement really depends more on the language and compiler than the hardware in this case.
Note that there are languages and situations where this might even take longer (interpretative languages for example)
Disclaimer:
This type of optimization isn't used much, mainly because it doesn't improve much (usually).
Try looking at other places (or others ways) to improve.
It shouldn't make any difference expect for making your code larger and have a larger memory footprint. For example, If the last if
is to be used then it would be similar to the last iteration in a while loop.... However you can still use something such as break or terminate the loop if a condition is met, which is similar to the having multiple if loops.
Make the code faster by optimizing other parts and profiling.
精彩评论