Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Converting a string to a real numeric value - what's faster?

by isync (Hermit)
on Sep 17, 2009 at 09:00 UTC ( [id://795818]=perlquestion: print w/replies, xml ) Need Help??

isync has asked for the wisdom of the Perl Monks concerning the following question:

Wrangling with lots of CGI stuff, it happens sometime that values like "7" end up in a variable, but are in fact not a real numeric integer value, but are regarded by perl as a string that happens to be a number.

Over the years I got the bad habit of thinking that in most cases it's 100% exchangeable, wheater its a number or a string. Perl is clever enough to handle it right, just like we humans do.

Now, yesterday, I ran into an issue that involved quite low-level transformations and as it turned out (thanks tofjw!), my string value was simply ignored and a *true* numeric value worked.

Now: what is the best way to transform a string to a true numeric value, in terms of speed/internal efficiency?
  • $number = $string + 0;
  • $number = int($string); # gladly, no floating-point number
  • Comment on Converting a string to a real numeric value - what's faster?

Replies are listed 'Best First'.
Re: Converting a string to a real numeric value - what's faster?
by mwah (Hermit) on Sep 17, 2009 at 09:18 UTC

    First, whats meant by:

    I ran into an issue that involved quite low-level transformations and as it turned out

    To your comparison: The code sequence (*)

    $string='7'; $num=$string+0
    gives (*):
    C:\>perl -MO=Concise,-exec -e"$string='7';$num=$string+0" 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> const[PV "7"] s 4 <#> gvsv[*string] s 5 <2> sassign vKS/2 6 <;> nextstate(main 1 -e:1) v:{ 7 <#> gvsv[*string] s 8 <$> const[IV 0] s 9 <2> add[t4] sK/2 a <#> gvsv[*num] s b <2> sassign vKS/2 c <@> leave[1 ref] vKP/REFC

    and the sequence (**)

    $string='7'; $num=int($string)"

    gives (**)

    C:\>perl -MO=Concise,-exec -e"$string='7';$num=int($string)" 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> const[PV "7"] s 4 <#> gvsv[*string] s 5 <2> sassign vKS/2 6 <;> nextstate(main 1 -e:1) v:{ 7 <#> gvsv[*string] s 8 <1> int[t4] sK/1 9 <#> gvsv[*num] s a <2> sassign vKS/2 b <@> leave[1 ref] vKP/REFC -e syntax OK
    The Difference is (*)
    8 <$> const[IV 0] s 9 <2> add[t4] sK/2
    compared to (**)
    8 <1> int[t4] sK/1

    so there shouldn't be too much difference

    Regards

    mwa

      Thanks!
      What I learn from that is: if i am handling non-floating-point values, it's a bit more elegant and possibly a tick faster (one iteration less, 1-b vs 1-c) to use int(), otherwise the +0 trick isn't that much of a hack it seems to be.
        if i am handling non-floating-point values, it's a bit more elegant ... to use int()

        .. if it's worth the typed characters at all ;-)

        As you said before, Perl will convert the string into a number internally if the string isn't already converted (IV-Flag not set) and if the string is used in a numerical context.

        Of course, when using Perl-Guts (XS), you have to worry.

        regards

        mwa

Re: Converting a string to a real numeric value - what's faster?
by JavaFan (Canon) on Sep 17, 2009 at 09:08 UTC
    Now, yesterday, I ran into an issue that involved quite low-level transformations and as it turned out (thanks tofjw!), my string value was simply ignored and a *true* numeric value worked.
    Really? If that's true, then there's a bug in Perl. Perhaps you can show a code fragment that shows this behaviour?
    Now: what is the best way to transform a string to a true numeric value, in terms of speed/internal efficiency?
    You tell us. Run a benchmark and tell us. Note also that they aren't equivalent if $string contains, say "1.23". Of course, I would expect that the fastest solution is to do nothing, and to let perl decide if and when a conversion is needed. And way faster still would be the get rid of Perl, ditch the CGI interface, and write embedded C.

      "Really? If that's true, then there's a bug in Perl."

      No need to worry: it was a known bug in an XS module I use here, and the developer of the module already fixed it - but it steered my attention towards this string/number thing.

      "You tell us."

      Call me a lazy bum, but being the noob that I am at proper benchmarking, I thought a quick post and a question to people who know the Perl internals would be faster.
        to people who know the Perl internals

        perlmonks ne perl5-porters ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://795818]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-24 01:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found