home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / perl / 6972 < prev    next >
Encoding:
Text File  |  1992-11-14  |  1.4 KB  |  46 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!panther!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
  3. From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
  4. Subject: Re: push/pop and memory management
  5. Message-ID: <mcook.721675140@fendahl.dev.cdx.mot.com>
  6. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  7. Nntp-Posting-Host: fendahl.dev.cdx.mot.com
  8. Organization: Motorola Codex, Canton, Massachusetts
  9. References: <1682@rwja.umdnj.edu>
  10. Date: Fri, 13 Nov 1992 17:19:00 GMT
  11. Lines: 33
  12.  
  13. majidi@rwja.umdnj.edu (Masoud Majidi) writes:
  14.  
  15. >Dear netters,
  16.  
  17. >I have a small perl code whose memory requirement was getting out of
  18. >hand. After studying the code for a while I am convinced that push/pop
  19. >is not working the way I expected. To prove the point I wrote a small
  20. >program which does nothing but pushing and popping from an array:
  21.  
  22. >for (1 .. 10000)
  23. >{
  24. >    push (@ary, 13);
  25. >    pop (@ary);
  26. >}
  27.  
  28. >And I checked the memory allocated to this code before and after the
  29. >loop. To my surprise there was more 2.5 Meg difference. Even if the pop
  30. >calls don't free the space I still don't understand how pushing 10,000
  31. >entries can increase the space by 2.5 Megs. I would appreciate it very
  32. >much if somebody would explain the above behavior.
  33.  
  34. Try this:
  35.  
  36. for ($i = 1; $i <= 1000; $i++)
  37. {
  38.     push (@ary, 13);
  39.     pop (@ary);
  40. }
  41.  
  42. I think you'll find it uses much less memory.  The 1..1000 construct builds a
  43. temporary array of 1000 elements, and then 'for' iterates on it.
  44.  
  45. Michael.
  46.