Conserving memory - was SPUG: Fw: greetings

Jeremy Devenport jeremy at weezel.com
Mon Oct 22 01:42:33 CDT 2001


Replying to two messages at once...

For reference I was doing all my comparisions on perl 5.6.1 on OpenBSD.

You're right, doing the defined check for named variables came in for
5.005. It's been a while since I was on anything older than that. See
the change description below (apparently perl 5 uses Perforce or
something similar for source management instead of CVS).

I'm going to try out some benchmarks with the sysread and lexical
variations tomorrow, if my attention span hasn't run out. I suspect that
you're right and that sysread/syswrite will be faster but I think I've
reached my personal limit of efficiency vs. readability.


>From Changes5.005:
________________________________________________________________________
____
[   949] By: TimBunce                              on 1998/05/14
15:11:30
        Log: 
             Title:  "while($x=<>) no longer warns (implicit defined
added)"
             From:  Nick Ing-Simmons <nik at tiuk.ti.com>
             Msg-ID:  <199805051035.LAA27365 at pluto.tiuk.ti.com>
             Files:  MANIFEST op.c t/op/defins.t
     Branch: maint-5.004/perl
           + t/op/defins.t
           ! MANIFEST op.c



Jeremy


-----Original Message-----
From: owner-spug-list at pm.org [mailto:owner-spug-list at pm.org] On Behalf
Of Jason Lamport
Sent: Sunday, October 21, 2001 8:16 PM
To: spug-list at pm.org
Subject: RE: Conserving memory - was SPUG: Fw: greetings


At 11:27 AM -0700 10/21/01, Jeremy Devenport wrote:
>
>Here's what I learned:
>
>1) You don't need to check for defined inside a while loop condition if

>it is of the form <FILE> or $var = <FILE>. Perl does this for you.

Is this true for all versions of Perl?  I seem vaguely to recall that 
this was a fairly recent (post 5.004) enhancement.  But I could be 
mis-remembering.

-jason


-----Original Message-----
From: owner-spug-list at pm.org [mailto:owner-spug-list at pm.org] On Behalf
Of El JoPe Magnifico
Sent: Sunday, October 21, 2001 3:25 PM
To: Seattle Perl Users Group
Subject: SPUG: RE: Conserving memory


On Sun, 21 Oct 2001, Jeremy Devenport wrote:
> 1) You don't need to check for defined inside a while loop condition 
> if it is of the form <FILE> or $var = <FILE>. Perl does this for you.
>
> You can use the B::Deparse module to take the opcodes back to perl and

> look if you don't believe me. Try running perl -MO=Deparse 
> comparision.txt and compare four_a and four_b before and after. Or if 
> you are source code oriented look at Perl_newWHILEOP in perl's op.c.

When did that behavior change?  while(<FILE>) has always checked for
defined, but while($var=<FILE>) _used_ to check for truth instead.

> In conclusion, this was the most efficient (memory & speed) way I 
> found in perl to output a file is: print while <FILE>;

I suspect that it'd be even faster to read in fixed-size chunks:

  syswrite(STDOUT, $_) while sysread(FILE, $_, 1024);

Which lets you skip parsing of the input for line endings, while still
saving you from slurping the whole file into memory.  Note that
sysread() and syswrite() have some gotchas associated with mixing with
other IO, e.g. print(), that I don't totally understand.  If you need to
do any other IO inside your while() loop, then the alternative is:

  print while read(FILE, $_, 1024);

I'd be interested to see benchmark comparisons. (sorry, in a rush now)
  -jp



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your
Email-address  For daily traffic, use spug-list for LIST ;  for weekly,
spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/




 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/





More information about the spug-list mailing list