473,289 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

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 7241
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cmelnick | last post by:
I have a custom class that basically consists of two elements, a name (String) and value (object). I am a bit confused on how to clone or make a copy of an instance of my object. If I have: ...
1
by: tnhoe | last post by:
Hi, <Form method='post' action="next.htm?btn="+"this.myform.myobj.value"> What is the correct syntax for above ? Regards Hoe
12
by: opistobranchia | last post by:
I was on a job interview and the interviewer asked what "this" meant. I replied it represented this class like cout << a or cout << this.a then he asked what this meant if it was static? I...
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
1
by: feng | last post by:
I need to create cookies in user's machine from my ASP.Net code and I also need to read them. How do I do that? What is the API (name space?) for this in APS.Net? Thanks
2
by: Sam | last post by:
I have multiple screens during a registration process. I have noticed that most BIG registration websites are not using "hidden text input objects" to carry the information from one page to...
8
by: semedao | last post by:
Hi I have some Queue object that I enqueue other objects into it. I want to check if someobject already exists before enqueue , using the Contains method but italways return false. in the object...
1
by: dhtmlkitchen | last post by:
I think this is not possible... var fnProto = Function().prototype; var badAss = { constructor : "Chuck Norris" }; result = ; function isConstructorEnumerable( ocean, pacific ) {
34
by: Jorge | last post by:
Why is it listed here : http://www.crockford.com/javascript/recommend.html under "deprecation" ? Without it, how is an anonymous function() going to call itself (recursively) ? I use to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.