469,271 Members | 1,047 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

What is faster? DirectCast(myObj, Integer) or Convert.ToInt32(myObj) ???

Hello,
what has the better performance and what are you using?

Dim myObj As Object = 70
a) Dim myInt As Integer = DirectCast(myObj, Integer)
b) Dim myInt As Integer = Convert.ToInt32(myObj)

Thanks,
Andreas
Nov 17 '05 #1
4 7054
Hello John,
here are my measurements:

Convert.ToInt32 0,37
DirectCast(myObj, Integer) 0,04

Convert.ToString(myObj) 0,42
DirectCast(myObj, String) 0,07
CStr(myObj) 0,50
CType(myObj, String) 0,50

So DirectCast is much faster. What do you use?

Regards,
Andreas
"John Saunders" <jo***********@surfcontrol.com> schrieb im Newsbeitrag
news:es**************@TK2MSFTNGP10.phx.gbl...
I use Option Strict and don't have the problem very often.

DirectCast should be faster in general, since the object _is_ an integer.

To find out whether it's faster in your particular case, why not write a
little test program which saves the start time, performs the operation
100000 times, then compares the start time to the end time? It would allow
you to test the performance of anything you like.

--
John Saunders
Internet Engineer
jo***********@surfcontrol.com

"Andreas Klemt" <ak******@hotmail.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
Hello,
what has the better performance and what are you using?

Dim myObj As Object = 70
a) Dim myInt As Integer = DirectCast(myObj, Integer)
b) Dim myInt As Integer = Convert.ToInt32(myObj)

Thanks,
Andreas


Nov 17 '05 #2
Andreas, how many times do you plan on posting the same question? You've
asked it 4 times since yesterday, always slightly differently..and EACH AND
EVERY TIME someone has provided you with a quality answer.

Did you read "Conversion Functions, CType, DirectCast, and System.Convert"
at
http://msdn.microsoft.com/library/de...tinternals.asp
(as was previously provided)?? It provides a microsoft recommentation as to
which to use...what more could you possibly want?

Karl

"Andreas Klemt" <ak******@hotmail.com> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
Hello John,
here are my measurements:

Convert.ToInt32 0,37
DirectCast(myObj, Integer) 0,04

Convert.ToString(myObj) 0,42
DirectCast(myObj, String) 0,07
CStr(myObj) 0,50
CType(myObj, String) 0,50

So DirectCast is much faster. What do you use?

Regards,
Andreas
"John Saunders" <jo***********@surfcontrol.com> schrieb im Newsbeitrag
news:es**************@TK2MSFTNGP10.phx.gbl...
I use Option Strict and don't have the problem very often.

DirectCast should be faster in general, since the object _is_ an integer.
To find out whether it's faster in your particular case, why not write a little test program which saves the start time, performs the operation
100000 times, then compares the start time to the end time? It would allow you to test the performance of anything you like.

--
John Saunders
Internet Engineer
jo***********@surfcontrol.com

"Andreas Klemt" <ak******@hotmail.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
Hello,
what has the better performance and what are you using?

Dim myObj As Object = 70
a) Dim myInt As Integer = DirectCast(myObj, Integer)
b) Dim myInt As Integer = Convert.ToInt32(myObj)

Thanks,
Andreas



Nov 17 '05 #3
Hello Karl,
I was confused about DirectCast because I never heard about it.
Now after I tested it, everything is clear.

Thanks to you for the link!!

Best Regards,
Andreas
"Karl Seguin" <kseguin##crea.ca> schrieb im Newsbeitrag
news:uQ**************@TK2MSFTNGP09.phx.gbl...
Andreas, how many times do you plan on posting the same question? You've
asked it 4 times since yesterday, always slightly differently..and EACH AND EVERY TIME someone has provided you with a quality answer.

Did you read "Conversion Functions, CType, DirectCast, and System.Convert"
at
http://msdn.microsoft.com/library/de...tinternals.asp (as was previously provided)?? It provides a microsoft recommentation as to which to use...what more could you possibly want?

Karl

"Andreas Klemt" <ak******@hotmail.com> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
Hello John,
here are my measurements:

Convert.ToInt32 0,37
DirectCast(myObj, Integer) 0,04

Convert.ToString(myObj) 0,42
DirectCast(myObj, String) 0,07
CStr(myObj) 0,50
CType(myObj, String) 0,50

So DirectCast is much faster. What do you use?

Regards,
Andreas
"John Saunders" <jo***********@surfcontrol.com> schrieb im Newsbeitrag
news:es**************@TK2MSFTNGP10.phx.gbl...
I use Option Strict and don't have the problem very often.

DirectCast should be faster in general, since the object _is_ an integer.
To find out whether it's faster in your particular case, why not
write
a little test program which saves the start time, performs the operation
100000 times, then compares the start time to the end time? It would allow you to test the performance of anything you like.

--
John Saunders
Internet Engineer
jo***********@surfcontrol.com

"Andreas Klemt" <ak******@hotmail.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
> Hello,
> what has the better performance and what are you using?
>
> Dim myObj As Object = 70
> a) Dim myInt As Integer = DirectCast(myObj, Integer)
> b) Dim myInt As Integer = Convert.ToInt32(myObj)
>
> Thanks,
> Andreas
>
>



Nov 17 '05 #4
C# :-)

In C#, I use (int) myObj, which is about the same thing as DirectCast.

--
John Saunders
Internet Engineer
jo***********@surfcontrol.com

"Andreas Klemt" <ak******@hotmail.com> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
Hello John,
here are my measurements:

Convert.ToInt32 0,37
DirectCast(myObj, Integer) 0,04

Convert.ToString(myObj) 0,42
DirectCast(myObj, String) 0,07
CStr(myObj) 0,50
CType(myObj, String) 0,50

So DirectCast is much faster. What do you use?

Regards,
Andreas
"John Saunders" <jo***********@surfcontrol.com> schrieb im Newsbeitrag
news:es**************@TK2MSFTNGP10.phx.gbl...
I use Option Strict and don't have the problem very often.

DirectCast should be faster in general, since the object _is_ an integer.
To find out whether it's faster in your particular case, why not write a little test program which saves the start time, performs the operation
100000 times, then compares the start time to the end time? It would allow you to test the performance of anything you like.

--
John Saunders
Internet Engineer
jo***********@surfcontrol.com

"Andreas Klemt" <ak******@hotmail.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
Hello,
what has the better performance and what are you using?

Dim myObj As Object = 70
a) Dim myInt As Integer = DirectCast(myObj, Integer)
b) Dim myInt As Integer = Convert.ToInt32(myObj)

Thanks,
Andreas



Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by cmelnick | last post: by
12 posts views Thread by opistobranchia | last post: by
43 posts views Thread by Mountain Bikn' Guy | last post: by
1 post views Thread by feng | last post: by
2 posts views Thread by Sam | last post: by
34 posts views Thread by Jorge | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.