HighTechTalks DotNet Forums  

DirectCast of a string or .ToString(): Which is Faster?

Dotnet Framework (Performance) microsoft.public.dotnet.framework.performance


Discuss DirectCast of a string or .ToString(): Which is Faster? in the Dotnet Framework (Performance) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default DirectCast of a string or .ToString(): Which is Faster? - 03-24-2006 , 01:08 PM






Let's say I have a dictionary where all the values are strings. Does
anyone know if it is faster to say:

dim s as string = directcast(_dictionary(key), string)

or

dim s as string = _dictionary(key).ToString()

One of the nice things about using .ToString on an object reference is
when working with a db. You know the column is char or varchar, but if
it is nullable, the underlying type could be a DBNULL or string.
Calling .ToString() on a DBNULL returns "", which is an acceptable
response in most situations.

Thanks,
Eric Marvets
MVP - Visual Developer, Security


Reply With Quote
  #2  
Old   
Alvin Bruney - ASP.NET MVP
 
Posts: n/a

Default Re: DirectCast of a string or .ToString(): Which is Faster? - 03-24-2006 , 02:06 PM






toString is implemented in an optimized way in the framework, it should be
faster than the overhead of directcast

<emarvets (AT) gmail (DOT) com> wrote

Quote:
Let's say I have a dictionary where all the values are strings. Does
anyone know if it is faster to say:

dim s as string = directcast(_dictionary(key), string)

or

dim s as string = _dictionary(key).ToString()

One of the nice things about using .ToString on an object reference is
when working with a db. You know the column is char or varchar, but if
it is nullable, the underlying type could be a DBNULL or string.
Calling .ToString() on a DBNULL returns "", which is an acceptable
response in most situations.

Thanks,
Eric Marvets
MVP - Visual Developer, Security




Reply With Quote
  #3  
Old   
AT
 
Posts: n/a

Default Re: DirectCast of a string or .ToString(): Which is Faster? - 03-24-2006 , 06:00 PM



How is it different than any other virtual method? I guess the real
question I should have posed is if it's quicker to call a virtual
method or cast to the concrete version from a pure performance
standpoint?

-Eric Marvets
MVP - Visual Developer, Security


Reply With Quote
  #4  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: DirectCast of a string or .ToString(): Which is Faster? - 03-25-2006 , 02:01 AM



<emarvets (AT) gmail (DOT) com> wrote:
Quote:
Let's say I have a dictionary where all the values are strings. Does
anyone know if it is faster to say:

dim s as string = directcast(_dictionary(key), string)

or

dim s as string = _dictionary(key).ToString()
Either way, the cost of looking the key up in the dictionary (which is
fairly fast in itself) is likely to dwarf the cost of either the cast
or the call to ToString. I find it very unlikely that you'll see any
significant performance difference at all. Use the version which is
most convenient/readable for you.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.