
h.m.brand at xs4all
Nov 6, 2009, 2:08 PM
Post #34 of 37
(181 views)
Permalink
|
On Thu, 5 Nov 2009 14:53:05 +0100, Boris Zentner <bzm [at] 2bz> wrote: > Hi, > > I use > > $x = pack 'x10000'; > $x = ''; > > which is 10 times faster than creating a list. So SvGROW looks > overdone to me. :) Next version of Data::Peek will have DGrow (), just as a proof of concept: $ perl -MDP -e'my$x="";DGrow($x,10000);DDump$x' SV = PV(0x8265048) at 0x8268310 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x82869c0 ""\0 CUR = 0 LEN = 10000 --8<--- use strict; use warnings; use Data::Peek qw( DGrow ); use Benchmark qw( cmpthese ); cmpthese (-2, { pack => q{my $x = ""; $x = pack "x20000"; $x = "";}, op_x => q{my $x = ""; $x = "x" x 20000; $x = "";}, grow => q{my $x = ""; DGrow ($x, 20000); $x = "";}, }); -->8--- Rate op_x pack grow op_x 62127/s -- -59% -96% pack 152046/s 145% -- -91% grow 1622943/s 2512% 967% -- > Am 05.11.2009 um 08:48 schrieb H.Merijn Brand: > > > On Wed, 4 Nov 2009 20:59:50 +0000, Ben Morrow <ben [at] morrow> > > wrote: > > > >> Quoth h.m.brand [at] xs4all ("H.Merijn Brand"): > >>> > >>> IIRC I once suggested to allow length () to do preallocation: > >>> > >>> $ perl -wle'length($a) = 8000' > >>> Can't modify length in scalar assignment at -e line 1, at EOF > >>> Execution of -e aborted due to compilation errors. > >> > >> ~% perl -MDevel::Peek -e'my $x = ("x" x 10_000); $x = ""; Dump $x' > >> SV = PV(0x810109c) at 0x8103b10 > >> REFCNT = 1 > >> FLAGS = (PADMY,POK,pPOK) > >> PV = 0x8134004 ""\0 > >> CUR = 0 > >> LEN = 16380 > >> ~% > >> > >> so while lvalue length would be a little neater, it's not strictly > >> necessary. > > > > There is quite a big difference here. 'x' x 10_000 actually > > allocates a > > temporary space for the value to be used to initialize $x with, then > > copies the variable and resets the length afterwards. A lot of > > unneeded > > ops. > > > > $ perl -MO=Deparse -we'my $x = ("x" x 10_000); $x = ""' > > BEGIN { $^W = 1; } > > my $x = 'x' x 10000; > > $x = ''; > > -e syntax OK > > $ perl -MO=Concise -we'my $x = ("x" x 10_000); $x = ""' > > c <@> leave[1 ref] vKP/REFC ->(end) > > 1 <0> enter ->2 > > 2 <;> nextstate(main 1 -e:1) v:{ ->3 > > 7 <2> sassign vKS/2 ->8 > > 5 <2> repeat[t2] sKP/2 ->6 > > 3 <$> const(PV "x") s ->4 > > 4 <$> const(IV 10000) s ->5 > > 6 <0> padsv[$x:1,2] sRM*/LVINTRO ->7 > > 8 <;> nextstate(main 2 -e:1) v:{ ->9 > > b <2> sassign vKS/2 ->c > > 9 <$> const(PV "") s ->a > > a <0> padsv[$x:1,2] sRM* ->b > > -e syntax OK > > > > If possible, I would suggest to allow 'length ($x) = 10000' to be > > something ending in > > > > SvGROW ($x, 10000); > > > > one single op! -- H.Merijn Brand http://tux.nl Perl Monger http://amsterdam.pm.org/ using & porting perl 5.6.2, 5.8.x, 5.10.x, 5.11.x on HP-UX 10.20, 11.00, 11.11, 11.23, and 11.31, OpenSuSE 10.3, 11.0, and 11.1, AIX 5.2 and 5.3. http://mirrors.develooper.com/hpux/ http://www.test-smoke.org/ http://qa.perl.org http://www.goldmark.org/jeff/stupid-disclaimers/
|