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

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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


    Andreas Klemt Guest

  2. Similar Questions and Discussions

    1. #39954 [NEW]: ~ operator doesn't convert string to integer first
      From: olafvdspek at gmail dot com Operating system: Windows XP PHP version: 5.2.0 PHP Bug Type: Scripting Engine problem Bug...
    2. #39278 [NEW]: strpos($needle, $integer) returns 0 for certain values of $integer
      From: matt at raines dot me dot uk Operating system: GNU/Linux 2.4.27 PHP version: 5.1.6 PHP Bug Type: Strings related Bug...
    3. Convert float value to integer
      l have a simple question ...l want to convert value from float to interger exemple 5.399 convert to 5 or 5.79 convert to 6
    4. Re-convert Currency back to Integer?
      Database: Access 2000 How does one switch a converted currency format back to a plain integer in Asp. I have tried Cint but this produces an...
    5. Do I need to Convert with Convert.ToInt32(session("myNumber")) ?
      Hello, I have this session("myNumber") = 888 Dim intNumber As Integer a) intNumber = session("myNumber") b) intNumber =...
  3. #2

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

    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" <john.saunders@surfcontrol.com> schrieb im Newsbeitrag
    news:esZacafUDHA.1196@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
    > [email]john.saunders@surfcontrol.com[/email]
    >
    > "Andreas Klemt" <aklemt68@hotmail.com> wrote in message
    > news:ueVLz7eUDHA.2056@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
    > >
    > >
    >
    >

    Andreas Klemt Guest

  4. #3

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

    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
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchmicrosoftvisualbasicnetinternals.asp[/url]
    (as was previously provided)?? It provides a microsoft recommentation as to
    which to use...what more could you possibly want?

    Karl

    "Andreas Klemt" <aklemt68@hotmail.com> wrote in message
    news:OTUD1rfUDHA.2076@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" <john.saunders@surfcontrol.com> schrieb im Newsbeitrag
    > news:esZacafUDHA.1196@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
    > > [email]john.saunders@surfcontrol.com[/email]
    > >
    > > "Andreas Klemt" <aklemt68@hotmail.com> wrote in message
    > > news:ueVLz7eUDHA.2056@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
    > > >
    > > >
    > >
    > >
    >
    >

    Karl Seguin Guest

  5. #4

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

    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:uQduCwfUDHA.2456@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
    >
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchmicrosoftvisualbasicnetinternals.asp[/url]
    > (as was previously provided)?? It provides a microsoft recommentation as
    to
    > which to use...what more could you possibly want?
    >
    > Karl
    >
    > "Andreas Klemt" <aklemt68@hotmail.com> wrote in message
    > news:OTUD1rfUDHA.2076@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" <john.saunders@surfcontrol.com> schrieb im Newsbeitrag
    > > news:esZacafUDHA.1196@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
    > > > [email]john.saunders@surfcontrol.com[/email]
    > > >
    > > > "Andreas Klemt" <aklemt68@hotmail.com> wrote in message
    > > > news:ueVLz7eUDHA.2056@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
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    Andreas Klemt Guest

  6. #5

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

    C# :-)

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

    --
    John Saunders
    Internet Engineer
    [email]john.saunders@surfcontrol.com[/email]

    "Andreas Klemt" <aklemt68@hotmail.com> wrote in message
    news:OTUD1rfUDHA.2076@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" <john.saunders@surfcontrol.com> schrieb im Newsbeitrag
    > news:esZacafUDHA.1196@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
    > > [email]john.saunders@surfcontrol.com[/email]
    > >
    > > "Andreas Klemt" <aklemt68@hotmail.com> wrote in message
    > > news:ueVLz7eUDHA.2056@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
    > > >
    > > >
    > >
    > >
    >
    >

    John Saunders Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139