http://qs321.pair.com?node_id=600213


in reply to Garbage collection at subroutine return

I have nothing to add to liverpole's suggestion, but the way you're doing your benchmark test. There's a Benchmark module available, which is really easy to use. If you write a benchmark code with it once, you'll never write a time calculation again. It is useful to compare results too.

Here is a code that compares liverpole's suggestion and the way you tried it first:

use strict; use warnings; use Benchmark qw(:all); my $limit = 1_000; sub do_it_tcarmeli { my %do_it_tcarmeli; foreach my $i ( 0 .. $limit ) { $do_it_tcarmeli{$i} = 1; } return; } my %do_it_liverpole; sub do_it_liverpole { foreach my $i ( 0 .. $limit ) { $do_it_liverpole{$i} = 1; } return; } cmpthese( -10, { do_it_tcarmeli => \&do_it_tcarmeli, do_it_liverpole => \&do_it_liverpole, } );

Igor 'izut' Sutton
your code, your rules.

Replies are listed 'Best First'.
Re^2: Garbage collection at subroutine return
by Anno (Deacon) on Feb 15, 2007 at 19:32 UTC
    Ah, but you're measuring the total run time of the subs. That's probably going to swamp out the effect of garbage collection on return. tcarmeli took care to measure only the return time. Benchmark doesn't give much support for that kind of timing.

    Oh, and "code" (in the sense of program text) is a mass noun and doesn't take an indefinite article.

    Anno