public Int64 ReturnDifferenceA()
{
User[] arrayList;
Int64 firstTicks;
IList<User> userList;
Int64 secondTicks;
System.Diagnostics.Stopwatch watch;
userList = Enumerable
.Range(0, 1000)
.Select(currentItem => new User()).ToList();
arrayList = userList.ToArray();
watch = new Stopwatch();
watch.Start();
for (Int32 loopCounter = 0; loopCounter < arrayList.Count(); loopCounter++)
{
DoThings(arrayList[loopCounter]);
}
watch.Stop();
firstTicks = watch.ElapsedTicks;
watch.Reset();
watch.Start();
for (Int32 loopCounter = 0; loopCounter < arrayList.Count(); loopCounter++)
{
DoThings(arrayList[loopCounter]);
}
watch.Stop();
secondTicks = watch.ElapsedTicks;
return firstTicks - secondTicks;
}
As you can see, this is really simple. Create a list of users, force to an array, start a watch, loop the list through and call a method, stop watch. Repeat. Finish up by returning the difference from the first run and the second.
Now I'm calling with these:
differenceList = Enumerable
.Range(0, 50)
.Select(currentItem => ReturnDifferenceA()).ToList();
average = differenceList.Average();
differenceListA = Enumerable
.Range(0, 50)
.Select(currentItem => ReturnDifferenceA()).ToList();
averageA = differenceListA.Average();
differenceListB = Enumerable
.Range(0, 50)
.Select(currentItem => ReturnDifferenceA()).ToList();
averageB = differenceListB.Average();
Now the fun part is that all averages are positive by a relatively large amount, ranging from 150k to 300k ticks.
What I don't get is that I am going through the same list, the same way, with the same method and yet there is such a difference. Is there some kind of caching going on?
Another interesting thing is that if I iterate through the list BEFORE the first stop watch section, the averages are around 5k or so.