Hi,
How does the .NET compiler optimize it's code?
I have to do searches by a property in a SortedList with a key that is not
the property I have to make my search on.... Is there a performance
difference (in release or debug mode) between those listings? even if the
performance difference is monimal, I want to know which one is best or if
they end up being the same optimized code.
Listing 1:
SortedList<int, Thing> MyThings;
...
...
foreach (Thing TheThing in MyThings)
{
if (TheThing.StringProperty == SomeOtherString)
{
// Do something
}
}
Listing 2:
SortedList<int, Thing> MyThings;
SortedList<int, string> MyThingsStringProperty;
...
// Fill the MyThingsStringProperty once at the beginning and from time to
time when it changes (not very often)
...
...
foreach (string StringProperty in MyThingsStringProperty)
{
if (StringProperty == SomeOtherString)
{
// Do something
}
}
Thanks
ThunderMusic
Greg Young - 03 Aug 2006 22:07 GMT
Your second example would be ever so slightly faster on the search as it is
a bit less redirection but would be more costly on adds/deletes. You would
need to be doing alot more searches than updates/deletes to make it faster.
I am sure you have heard the saying "premature optimization is the root of
all evil". This is a great example of such. You are trying to optimize a
very small bit when you should be looking at the algorithm.
Maintaining an index into the original list would be much faster than either
of these as they are both O(n). If your lists are small this doesn't really
matter but if they are large you could do something along the lines of
keeping a table of strings back to their index value ... the two operations
would still generally be much faster than a single O(n) operation.
Cheers,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
> Hi,
> How does the .NET compiler optimize it's code?
[quoted text clipped - 36 lines]
>
> ThunderMusic
ThunderMusic - 04 Aug 2006 02:47 GMT
thanks a lot
> Your second example would be ever so slightly faster on the search as it
> is a bit less redirection but would be more costly on adds/deletes. You
[quoted text clipped - 58 lines]
>>
>> ThunderMusic