Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Ways to delete start of string

by moritz (Cardinal)
on May 24, 2008 at 16:28 UTC ( [id://688310]=note: print w/replies, xml ) Need Help??


in reply to Ways to delete start of string

The speed depends on the length of the string.
Here's my modified benchmark:
#!/perl/bin/perl # # bench.pl -- use strict; use warnings; use diagnostics; use Benchmark qw(:all); my $str = '|0' x (shift @ARGV || 100_000); sub caseone { $_ = $str; s/.//; } sub casetwo { $_ = $str; substr($_,0,1) = ''; } sub casethree { $_ = $str; $_ = reverse $_; chop; $_ = reverse $_; } sub casefour { $_ = $str; $_ = substr($_,1); } cmpthese(-2, { 'regex' => 'caseone', 'substr_mod' => 'casetwo', 'reverse' => 'casethree', 'susbtr_copy' => 'casefour', });
# string length 2 * 10: Rate reverse regex substr_mod susbtr_copy reverse 797484/s -- -29% -30% -41% regex 1125463/s 41% -- -1% -16% substr_mod 1131927/s 42% 1% -- -16% susbtr_copy 1344489/s 69% 19% 19% -- # string length 2 * 100_000: Rate reverse susbtr_copy regex substr_mod reverse 1385/s -- -51% -60% -80% susbtr_copy 2847/s 106% -- -17% -58% regex 3437/s 148% 21% -- -49% substr_mod 6771/s 389% 138% 97% -- # with 2 * 1e7: Rate reverse susbtr_copy regex substr_mod reverse 8.61/s -- -54% -58% -81% susbtr_copy 18.7/s 117% -- -8% -58% regex 20.4/s 137% 9% -- -54% substr_mod 44.6/s 418% 139% 119% --

The speed doesn't really depend on the perl version (I tried 5.8.8 and 5.10.0).

Replies are listed 'Best First'.
Re^2: Ways to delete start of string
by hsmyers (Canon) on May 24, 2008 at 20:25 UTC
    'reverse' seems fairly intuitive, longer the string the greater the work (times two in fact). 'regex' looks like it is seeking constant time of sorts. I'm clueless on the flip flop with 'substr_mod' and 'substr_copy'.

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
      If you really want to know, look in pp.c in the perl source, in blead it's in lines 3055 to 3223.

      I skimmed it quickly and still have no clue how it does things - too many macros, too little knowledge from my part.

        Hmmm... I'm perfectly willing to let sleeping curiosities lie.

        --hsm

        "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re^2: Ways to delete start of string (OOK)
by ikegami (Patriarch) on May 27, 2008 at 03:14 UTC

    The time needed to perform $x =~ s/.//; and substr($x,0,1) = ''; is not related to the length of $x thanks to the "OOK" optimization.

    Instead of

    1. allocating a new buffer,
    2. copying the the string to the new buffer minus the leading char,
    3. assigning the new buffer to the variable and
    4. freeing the old buffer

    those two operations

    1. increment the pointer to the buffer in the variable,
    2. assign 1 to the IV slot* of the variable if the OOK flag is off or
    3. increment the IV slot* of the variable if the OOK flag is on, and
    4. turn on the OOK flag

    The string is never copied. You can see this in effect in the following snippet:

    >perl -MDevel::Peek -e"my $x='abcdef'; Dump($x); substr($x,0,1)=''; Du +mp($x);" SV = PV(0x226104) at 0x2252e8 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x182ca64 "abcdef"\0 CUR = 6 LEN = 8 SV = PVIV(0x227134) at 0x2252e8 REFCNT = 2 FLAGS = (PADBUSY,PADMY,POK,OOK,pPOK) IV = 1 (OFFSET) PV = 0x182ca65 ( "a" . ) "bcdef"\0 CUR = 5 LEN = 7

    If POK is true and OOK is false, then
    string start = PV
    string length = CUR
    number bytes allocated = LEN
    start of buffer = PV

    If POK is true and OOK is true, then
    string start = PV
    string length = CUR
    number bytes allocated = LEN + IV
    start of buffer = PV - IV

    * — Nicholas Clark recently made a change to Perl so that the chopped bytes are used instead of the IV slot. That hasn't appeared in any Perl release yet.

    Update: Changed substr($x,0,1,''); to substr($x,0,1)=''; since the rest of the thread used the latter.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-04-23 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found