Rob Hanson wrote:

>> $string =~ s/^.(.*).$/$1/;
> 
> It's prettier, but it's very inefficient.  It's probably 20+ times slower
> then doing it in two steps.
> 
> It has to do with how Perl internally handled string data.  When you s///
> or substr() all it does is move a pointer.  
> 

this is an over generalization and is only true for a small set of 
operation. it only moves pointer when you are deleting stuff from the 
beginning:

[panda]$ perl -MDevel::Peek -e '$_=1234; s/.//; Dump $_'
SV = PVIV(0x804c700) at 0x804bfb8
  REFCNT = 1
  FLAGS = (POK,OOK,pPOK)
  IV = 1  (OFFSET)
  PV = 0x805c599 ( "1" . ) "234"\0
  CUR = 3
  LEN = 4
[panda]$

without going into too much detail (because this is a beginner list). notice 
the OFFSET flag is set for $_. $_ is essentially called an offset string 
because Perl "moves" the "start" of the string 1 byte forward without 
rewriting anything. This happens transparently to the user and it's very 
efficient. not every s/.// will result in an offset string though:

[panda]$ perl -MDevel::Peek -e '$_=1234; s/34//; Dump $_'
SV = PVIV(0x804c700) at 0x804bfb8
  REFCNT = 1
  FLAGS = (POK,pPOK)
  IV = 1234
  PV = 0x805c598 "12"\0
  CUR = 2
  LEN = 5

so is substr:

[panda]$ perl -MDevel::Peek -e '$_=1234; substr($_,2,2,''); Dump $_'
SV = PVIV(0x804c700) at 0x804bfb8
  REFCNT = 1
  FLAGS = (IOK,POK,pIOK,pPOK)
  IV = 1234
  PV = 0x805c5a8 "1234"\0
  CUR = 4
  LEN = 5

in both cases, there are no offset string because you are not removing stuff 
from the beginning.

david
-- 
$_=q,015001450154015401570040016701570162015401440041,,*,=*|=*_,split+local$";
map{~$_&1&&{$,<<=1,[EMAIL PROTECTED]||3])=>~}}0..s~.~~g-1;*_=*#,

goto=>print+eval

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to