home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!caen!destroyer!ubc-cs!dhami
- From: dhami@cs.ubc.ca (Mandeep S Dhami)
- Subject: >> LVALUE =~ EXPR ? (performance question)
- Message-ID: <1992Sep4.230608.28865@cs.ubc.ca>
- Sender: usenet@cs.ubc.ca (Usenet News)
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- References: <1992Aug29.053235.3664@yarra-glen.aaii.oz.au> <1992Sep4.050628.22356@yarra-glen.aaii.oz.au>
- Date: Fri, 4 Sep 92 23:06:08 GMT
- Lines: 69
-
- pem@yarra-glen.aaii.oz.au (Paul E. Maisano) writes:
- > $_ = $some_meaningfully_named_variable;
- > (tr/abc/xyz/, s/foo/bar/, s/BAR/puppy/);
-
- >But I might have been using $_ for something else.
- >Probably this is best:
- > { local($_) = ($some_meaningfully_named_variable);
- > tr/abc/xyz/;
- > s/foo/bar/;
- > s/BAR/puppy/;
- > }
-
- This got me thinking about the performance hit for an extra {} block.
- So, I defined three functions as below:
-
- sub one {
- $x='abcdeffoo';
- $x=~tr/abc/xyz/;
- $x=~s/foo/bar/o;
- $x=~s/def//o;
- }
-
- sub two {
- $x='abcdeffoo';
- { local($_)=$x; tr/abc/xyz/,s/foo/bar/o,s/def//o;}
- }
-
- sub three {
- $x='abcdeffoo';
- $tmp=$_; $_=$x;
- tr/abc/xyz/,s/foo/bar/o,s/def//o;
- $_=$tmp;
- }
-
- I expected &three to do better than &two (penalty for {}) and
- I was not sure about &one (according to "The Camel Book" using
- defaults does not (in general) improve perfomance, but some(?)
- case are optimized). I got the following results and am a
- little preplexed. Can someone explain the figures? (the script
- was similar to the camel book timer.pl(?)).
-
- $ time.pl
- [Iter ]cmd :user sys u+s
- [1000 ]&one :0.05 0.08 0.13
- [1000 ]&two :0.06 0 0.06
- [1000 ]&three :0.06 0 0.06
- $ time.pl
- [Iter ]cmd :user sys u+s
- [1000 ]&one :0.06 0.06 0.13
- [1000 ]&two :0.06 0 0.06
- [1000 ]&three :0.06 0 0.06
-
- $ time.pl
- [Iter ]cmd :user sys u+s
- [10000 ]&one :0.63 0.06 0.70
- [10000 ]&two :0.61 0.04 0.66
- [10000 ]&three :0.64 0 0.64
- $ time.pl
- [Iter ]cmd :user sys u+s
- [10000 ]&one :0.61 0.08 0.70
- [10000 ]&two :0.66 0 0.66
- [10000 ]&three :0.65 0 0.65
-
- Mandeep.
- __________________________________________________________________
- "I'm very brave generally," he went on in a low voice:
- "only to-day I happen to have a headache."
- -- Lewis Carroll
- __________________________________________________________________
-