home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5738 < prev    next >
Encoding:
Text File  |  1992-09-07  |  3.1 KB  |  81 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!caen!destroyer!ubc-cs!dhami
  3. From: dhami@cs.ubc.ca (Mandeep S Dhami)
  4. Subject: >> LVALUE =~ EXPR ? (performance question)
  5. Message-ID: <1992Sep4.230608.28865@cs.ubc.ca>
  6. Sender: usenet@cs.ubc.ca (Usenet News)
  7. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  8. References: <1992Aug29.053235.3664@yarra-glen.aaii.oz.au> <1992Sep4.050628.22356@yarra-glen.aaii.oz.au>
  9. Date: Fri, 4 Sep 92 23:06:08 GMT
  10. Lines: 69
  11.  
  12. pem@yarra-glen.aaii.oz.au (Paul E. Maisano) writes:
  13. >    $_ = $some_meaningfully_named_variable;
  14. >    (tr/abc/xyz/, s/foo/bar/, s/BAR/puppy/);
  15.  
  16. >But I might have been using $_ for something else.
  17. >Probably this is best:
  18. >    { local($_) = ($some_meaningfully_named_variable);
  19. >      tr/abc/xyz/;
  20. >      s/foo/bar/;
  21. >      s/BAR/puppy/;
  22. >    }
  23.  
  24. This got me thinking about the performance hit for an extra {} block.
  25. So, I defined three functions as below:
  26.  
  27. sub one {
  28.     $x='abcdeffoo';
  29.     $x=~tr/abc/xyz/;
  30.     $x=~s/foo/bar/o;
  31.     $x=~s/def//o;
  32. }
  33.  
  34. sub two {
  35.     $x='abcdeffoo';
  36.     { local($_)=$x; tr/abc/xyz/,s/foo/bar/o,s/def//o;}
  37. }
  38.  
  39. sub three {
  40.     $x='abcdeffoo';
  41.     $tmp=$_; $_=$x;
  42.     tr/abc/xyz/,s/foo/bar/o,s/def//o;
  43.     $_=$tmp;
  44. }
  45.  
  46. I expected &three to do better than &two (penalty for {}) and
  47. I was not sure about &one (according to "The Camel Book" using
  48. defaults does not (in general) improve perfomance, but some(?)
  49. case are optimized). I got the following results and am a
  50. little preplexed. Can someone explain the figures? (the script
  51. was similar to the camel book timer.pl(?)).
  52.  
  53. $ time.pl
  54. [Iter  ]cmd                                                   :user sys  u+s 
  55. [1000  ]&one                                                  :0.05 0.08 0.13
  56. [1000  ]&two                                                  :0.06 0    0.06
  57. [1000  ]&three                                                :0.06 0    0.06
  58. $ time.pl
  59. [Iter  ]cmd                                                   :user sys  u+s 
  60. [1000  ]&one                                                  :0.06 0.06 0.13
  61. [1000  ]&two                                                  :0.06 0    0.06
  62. [1000  ]&three                                                :0.06 0    0.06
  63.  
  64. $ time.pl
  65. [Iter  ]cmd                                                   :user sys  u+s 
  66. [10000 ]&one                                                  :0.63 0.06 0.70
  67. [10000 ]&two                                                  :0.61 0.04 0.66
  68. [10000 ]&three                                                :0.64 0    0.64
  69. $ time.pl
  70. [Iter  ]cmd                                                   :user sys  u+s 
  71. [10000 ]&one                                                  :0.61 0.08 0.70
  72. [10000 ]&two                                                  :0.66 0    0.66
  73. [10000 ]&three                                                :0.65 0    0.65
  74.  
  75. Mandeep.
  76. __________________________________________________________________
  77. "I'm very brave generally," he went on in a low voice:
  78. "only to-day I happen to have a headache."
  79.                                       -- Lewis Carroll
  80. __________________________________________________________________
  81.