2007-12-26

Code Performance -- The Slowest Option

I previously posted a screenshot showing the magnitude of difference between execution times for various ways of computing some total value.

Here's the relevant source code for the slowest option, ListIntFor:


for (int i = 0; i < Max; i++)
{
for (int iter = 0; iter < li.Count; iter++)
{
total += li[iter];
}
}


If you simply change the li.Count to a local reference (int itemCount = li.Count;) outside the outer loop, you'll get a ~15% speedup.

0 comments: