home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!panther!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
- From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
- Subject: Re: push/pop and memory management
- Message-ID: <mcook.721675140@fendahl.dev.cdx.mot.com>
- Sender: news@merlin.dev.cdx.mot.com (USENET News System)
- Nntp-Posting-Host: fendahl.dev.cdx.mot.com
- Organization: Motorola Codex, Canton, Massachusetts
- References: <1682@rwja.umdnj.edu>
- Date: Fri, 13 Nov 1992 17:19:00 GMT
- Lines: 33
-
- majidi@rwja.umdnj.edu (Masoud Majidi) writes:
-
- >Dear netters,
-
- >I have a small perl code whose memory requirement was getting out of
- >hand. After studying the code for a while I am convinced that push/pop
- >is not working the way I expected. To prove the point I wrote a small
- >program which does nothing but pushing and popping from an array:
-
- >for (1 .. 10000)
- >{
- > push (@ary, 13);
- > pop (@ary);
- >}
-
- >And I checked the memory allocated to this code before and after the
- >loop. To my surprise there was more 2.5 Meg difference. Even if the pop
- >calls don't free the space I still don't understand how pushing 10,000
- >entries can increase the space by 2.5 Megs. I would appreciate it very
- >much if somebody would explain the above behavior.
-
- Try this:
-
- for ($i = 1; $i <= 1000; $i++)
- {
- push (@ary, 13);
- pop (@ary);
- }
-
- I think you'll find it uses much less memory. The 1..1000 construct builds a
- temporary array of 1000 elements, and then 'for' iterates on it.
-
- Michael.
-