in reply to Re^2: Don't treat your numbers as strings, or Interpolation is worse than you might think
in thread Don't treat your numbers as strings, or Interpolation is worse than you might think

++Joost and diotalevi. Thank you :) After reading the documentation of Devel::Peek i understood that among others there are three types of simple scalar value in Perl—PV (string), IV (integer) and PVIV (both). Of course i tried to compare the speed of them (actually, as you noticed, the speed of copying them). And I got a expected result:
Rate PVIV PV IV PVIV 2065/s -- -1% -64% PV 2084/s 1% -- -64% IV 5806/s 181% 179% --
The difference between PV and PVIV is so small that i did not notice it in my benchmarks and thought that the numbers are CONVERTED to strings :)

The code of the benchmark is here.

#!/usr/bin/perl use warnings; use strict; use Benchmark qw/:hireswallclock cmpthese/; my @ary = (0..10); #only IV my $len = $#ary; my @ary2 = @ary; my @ary3 =qw/0 1 2 3 4 5 6 7 8 9 10/; #only PV print "ARRAY: @ary2\n"; # IV -> PVIV cmpthese (-3, { 'IV' => sub { foreach my $i (0..$len-1) { foreach my $j ($i+1..$len) { my @permuted = @ary; @permuted[$i, $j] = @permuted[$j, $i]; } } }, 'PVIV' => sub { foreach my $i (0..$len-1) { foreach my $j ($i+1..$len) { my @permuted = @ary2; @permuted[$i, $j] = @permuted[$j, $i]; } } }, 'PV' => sub { foreach my $i (0..$len-1) { foreach my $j ($i+1..$len) { my @permuted = @ary3; @permuted[$i, $j] = @permuted[$j, $i]; } } }, });
UPD: updated according to diotalevi's note :)

     s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print
  • Comment on Re^3: Don't treat your numbers as strings, or Interpolation is worse than you might think
  • Select or Download Code

Replies are listed 'Best First'.
Re^4: Don't treat your numbers as strings, or Interpolation is worse than you might think
by diotalevi (Canon) on Jul 22, 2006 at 16:23 UTC

    There's more than just that. Check out the nice ASCII art in B and the pretty pictures at PerlGuts Illustrated.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^4: Don't treat your numbers as strings, or Interpolation is worse than you might think
by Marsel (Sexton) on Jul 23, 2006 at 13:06 UTC
    It's a really interesting meditation, thanks !

    it left me with one question :
    when you type :
    print "ARRAY: @ary2\n"; # IV -> PVIV
    it converts your IV to PVIV.

    But, I would have thinked then that this :
    $_ += 0 foreach @ary2;
    executed on a PVIV, would keep it a PVIV. Why does it reverse it to an IV. It's a kind of rule internally ? When you make a numeric operation on a scalar, it convert it into an IV ? I mean, if there is a type which carry both (PVIV), it could have continue with it ? No ? Are there reasons other than the idea of using less memory/less timecode ?

    Thanks,
    Marsel
        The effect of removing this flag is very interesting—the value of the string stored in the variable is not deleted, but is "considered" incorrect and is not copied to new variables :)
        perl -MDevel::Peek -e'$x = 3; qq($x); $x+=1; Dump $x; $y=$x; Dump $y' SV = PVIV(0x15d75f4) at 0x15d57e4 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 4 PV = 0x1a53acc "3"\0 CUR = 1 LEN = 2 SV = PVIV(0x15d7604) at 0x15d64d4 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 4 PV = 0
        You see—the PV (value of the string) for $x was not updated by the numeric operation and was not copied to $y. So, being strict, the variables after numification become not pure IV, but PVIV storing only integer, looking much like simple IV, which causes much less work to copy :)

             s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print