[quote name='SteveDeFacto' timestamp='1314628755' post='4855064']
I was not aware duplicating code instead of calling a function multiple times would actually somehow make the program run slower...
This thread has generated a lot of noise, so I apologize if this has already been addressed.
Modern processors can operate on data much faster than they can load it from memory, and program instructions are included in this data; to execute code you first need to load it from memory. It's frequently the case that instructions you've executed once you'll execute again, so the processor stores those instructions in a faster kind of memory called an instruction cache, which has a finite size, so storing new instructions generally means dumping old instructions. If you duplicate code then the processor needs to load all those duplicated instructions separately from memory multiple times. In comparison, if you call the same function multiple times then it's more likely that the instructions will already been in cache and you'll get a performance boost compared to loading from main memory every time. And again, the cache is finite in size, so if you duplicate code segments manually you may also be pushing out of the cache instructions you'll want to use again soon.
That's the simple explanation of the concept anyways. There are also issues like multiple levels of cache, shared data and instruction caches, cache lines, associativity and so on. There are also times when duplicating code may create a local performance benefit; however, it's generally best to use a profiler or profile guided optimizer evaluate those conditions - and those tools are much better at telling you what code should have be inlined but weren't than they are at telling you what code was duplicated but shouldn't have been.
[/quote]
That's good information and I'll take that into account whenever I get around to optimizing.