Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Performance / August 2006

Tip: Looking for answers? Try searching our database.

Compiler optimization, which one is faster? or are they even?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ThunderMusic - 03 Aug 2006 21:18 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.