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 / CLR / March 2005

Tip: Looking for answers? Try searching our database.

Performance difference of switch statement or delegate?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jim.wiese@gmail.com - 09 Mar 2005 15:45 GMT
I recently read the MSDN article by Jan Gray at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/f
astmanagedcode.asp


... and was impressed by the analysis of the types of CLR statements,
but I've run into an interesting problem.  I have a scenario where I
could use a switch statement or abstract an interface to compare a
bunch of items by type.  This method will be call a *huge* number of
times, so I'm a bit sensitive to the relative performance (ie I'm only
going to use C#, not C++).  Basically, the options I have are similar
to the following:

//
// NOTE: This is pseudo-code and wasn't compiled
//
int   doThisByType( object compareA, object compareB ) {

  switch( typeof( compareA ) ) {
    case Int32:
       return compareInt32( compareA, compareB ) ;
       break ;
    case Int64:
       return compareInt64( compareA, compareB ) ;
       break ;
    case string:
       return compareString( compareA, compareB ) ;
       break ;
  }
}

// The only problem is that if I'm calling this method often
// for the same known type, then I'm constantly evaluating the
// switch statement. (ie paying the time for the comparison)

//
// Or, I could abstract the method like this:
//

public interface ITypeComparer
{
 int   Compare( object valueA, object valueB ) ;
}

public class Int32Comparer : ITypeComparer
{
 int   Compare( object valueA, object valueB )
 {
    Int32  valueAInt32 = (Int32)valueA ;
    Int32  valueBInt32 = (Int32)valueB ;

    if ( valueA < valueB )
      return -1 ;
    else if ( valueA == valueB )
      return 0 ;
    else
      return +1 ;
 }
}

public class Int64Comparer : ITypeComparer
{
 int   Compare( object valueA, object valueB )
 {
    Int64  valueAInt32 = (Int64)valueA ;
    Int64  valueBInt32 = (Int64)valueB ;

    if ( valueA < valueB )
      return -1 ;
    else if ( valueA == valueB )
      return 0 ;
    else
      return +1 ;
 }
}

public class StringComparer : ITypeComparer
{
 int   Compare( object valueA, object valueB )
 {
    string valueAInt32 = (string)valueA ;
    string valueBInt32 = (string)valueB ;

    return string.Compare( valueA, valueB, true ) ;
 }
}

public class Comparer {
 public static int   doThisByType( ITypeComparer comparer, object
compareA, object compareB ) {

    return comparer.Compare( compareA, compareB );
 }
}

//
// END CODE
//

But basically, I was concerned about the penalty for the interface
method invokation.  In the article mentioned above, an interface method
call is ~10x slower than a class method call.  I was wondering if the
switch statement evaluation has less than a ~10x performance hit for
under 10 conditions?  Any ideas?

Thanks,

Jim
Sean Hederman - 09 Mar 2005 18:57 GMT
Just time it. You've got the pseudo-code, toss it into a test harness and
iterate it a few hundred thousand time and see. That'll give you a far
better answer than anything else.

>I recently read the MSDN article by Jan Gray at:
>
[quoted text clipped - 103 lines]
>
> Jim
Christoph Nahr - 10 Mar 2005 06:55 GMT
That seems like an excellent case for profiling.  There's really no
way to tell what will be faster.  With *many* case statements the
delegates will be faster, no doubt, but with just a few?  Who knows?
Signature

http://www.kynosarges.de

Dinh Duy Tran - 11 Mar 2005 14:28 GMT
Jim,

Have you looked at the overload method yet?

int   doThisByType( Int32 compareA,  Int32 compareB )
int   doThisByType( Int64 compareA,  Int64 compareB )
int   doThisByType( string compareA,  string compareB )
...

This will give you a better performance compare with the other two.

Duy

CSDP/MCSD

>I recently read the MSDN article by Jan Gray at:
>
[quoted text clipped - 103 lines]
>
> Jim
jim.wiese@gmail.com - 11 Mar 2005 18:54 GMT
Duy,

 Thanks for the input, but this would requite compile time knowledge
of the data type.  What I need is to do the fastest *runtime* operation
given two objects of any value type.

 -Jim
cody - 16 Mar 2005 17:42 GMT
Since all standardtypes already implements the IComparable interface and you
can also use this interface with ouyr own types why not simply use:

int   doThisByType( object compareA, object compareB )
{
   return ((IComparable)compareA).CompareTo(compareB );
}

> I recently read the MSDN article by Jan Gray at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/fastmanagedcode.asp

> ... and was impressed by the analysis of the types of CLR statements,
> but I've run into an interesting problem.  I have a scenario where I
[quoted text clipped - 99 lines]
>
> Jim

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.