in reply to Most efficient way to print a file?
I'm returning it in a function call...
In case the string is huge and you want to avoid some temporary memory allocation (which could push up the peak memory usage of the program), you could return a reference to the scalar:
#!/usr/bin/perl sub mem { print "$_[0]:\n"; system "/bin/ps", "-osize,vsize", $$; } sub foo { mem(0); my $data; $data .= "xxxxxxxxxx" for 1..1_000_000; mem(1); return \$data; # return reference } my $r = foo(); # then get at the data using $$r mem(2); __END__ $ ./880173.pl 0: SZ VSZ 612 19804 1: SZ VSZ 10468 29660 2: SZ VSZ 10468 29660
Using the same snippet, but with return $data, you'd get:
0: SZ VSZ 612 19804 1: SZ VSZ 10468 29660 2: SZ VSZ 20236 39428
As you can see, the process size has grown additionally by about the size of $data.
Whether this really saves anything overall depends on what else the program is doing to actually print the string, etc.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Most efficient way to print a file?
by DreamT (Pilgrim) on Jan 03, 2011 at 13:52 UTC | |
by JavaFan (Canon) on Jan 03, 2011 at 14:26 UTC | |
by DreamT (Pilgrim) on Jan 05, 2011 at 12:29 UTC | |
by JavaFan (Canon) on Jan 05, 2011 at 13:07 UTC | |
by Anonyrnous Monk (Hermit) on Jan 03, 2011 at 14:00 UTC |