Say you need to have a list/array of integers which you need iterate frequently, and I mean extremely often. The reasons may vary, but say it's in the heart of the inner most loop of a high volume processing.
In general, one would opt for using Lists (List) due to their flexibility in size. On top of that, msdn documentation claims Lists use an array internally and should perform just as fast (a quick look with Reflector confirms this). Neverless, there is some overhead involved.
Did anyone actually measure this? would iterating 6M times through a list take the same time as an array would?
Short answer:
More detailed answer you will find by the following link: https://stackoverflow.com/a/29263914/4423545
I think the performance will be quite similar. The overhead that is involved when using a List vs an Array is, IMHO when you add items to the list, and when the list has to increase the size of the array that it's using internally, when the capacity of the array is reached.
Suppose you have a List with a Capacity of 10, then the List will increase it's capacity once you want to add the 11th element. You can decrease the performance impact by initializing the Capacity of the list to the number of items it will hold.
But, in order to figure out if iterating over a List is as fast as iterating over an array, why don't you benchmark it ?
On my system; iterating over the array took 33msec; iterating over the list took 66msec.
To be honest, I didn't expect that the variation would be that much. So, I've put my iteration in a loop: now, I execute both iteration 1000 times. The results are:
Now, the variation is not that large anymore, but still ...
Therefore, I've started up .NET Reflector, and the getter of the indexer of the List class, looks like this:
As you can see, when you use the indexer of the List, the List performs a check whether you're not going out of the bounds of the internal array. This additional check comes with a cost.
Since I had a similar question this got me a fast start.
My question is a bit more specific, 'what is the fastest method for a reflexive array implementation'
The testing done by Marc Gravell shows a lot, but not exactly access timing. His timing include the looping over the array's and lists as well. Since I also came up with a third method that I wanted to test, a 'Dictionary', just to compare, I extended hist test code.
Firts, I do a test using a constant, which gives me a certain timing including the loop. This is a 'bare' timing, excluding the actual access. Then I do a test with accessing the subject structure, this gives me and 'overhead included' timing, looping and actual access.
The difference between 'bare' timing and 'overhead indluded' timing gives me an indication of the 'structure access' timing.
But how accurate is this timing? During the test windows will do some time slicing for shure. I have no information about the time slicing but I asume it is evenly distributed during the test and in the order of tens of msec which means that the accuracy for the timing should be in the order of +/- 100 msec or so. A bit rough estimate? Anyway a source of a systematic mearure error.
Also, the tests were done in 'Debug' mode with no optimalisation. Otherwise the compiler might change the actual test code.
So, I get two results, one for a constant, marked '(c)', and one for access marked '(n)' and the difference 'dt' tells me how much time the actual access takes.
And this are the results:
With better estimates on the timing errors (how to remove the systematic measurement error due to time slicing?) more could be said about the results.
It looks like List/foreach has the fastest access but the overhead is killing it.
The difference between List/for and List/foreach is stange. Maybe some cashing is involved?
Further, for access to an array it does not matter if you use a
for
loop or aforeach
loop. The timing results and its accuracity makes the results 'comparible'.Using a dictionary is by far the slowest, I only considered it because on the left side (the indexer) I have a sparse list of integers and not a range as is used in this tests.
Here is the modified test code.
Since List<> uses arrays internally, the basic performance should be the same. Two reasons, why the List might be slightly slower:
To check if it makes any difference for you, it's probably best adjust the posted timing functions to a list of the size you're planning to use and see how the results for your special case are.
Here's one that uses Dictionaries, IEnumerable:
[See also this question]
I've modified Marc's answer to use actual random numbers and actually do the same work in all cases.
Results:
Compiled as Release under VS 2008 SP1. Running without debugging on a Q6600@2.40GHz, .NET 3.5 SP1.
Code: