in reply to Re: Benchmarking File Retrevial
in thread Benchmarking File Retrevial

I'm getting my Benchmark syntax from Programming Perl. Here's the example in the book:
use Benchmark qw/countit cmpthese/; sub run($) { countit(5, @_) } for $size (2, 200, 20_000) { $s = "." x $len; print "\nDATASIZE = $size\n"; cmpthese { chop2 => run q{ $t = $s; chop $t; chop $t; }, subs => run q{ ($t = $s) =~ s/..\Z//s; }, lsubstr => run q{ $t = $s; substr($t, -2) = ''; }, rsubstr => run q{ $t = substr($s, 0, length($s)-2); }, }; }
Reinitializing @hitwords is a good idea. I'll try it. Also, I thought the only difference between while($word = <WORDS>){ and while(<WORDS>){ was that in the latter, the line was stored in !_. I haven't been able to find any documentation to the contrary, although I'd believe there might be a difference.

Replies are listed 'Best First'.
Re: Re: Re: Benchmarking File Retrevial
by chromatic (Archbishop) on Dec 15, 2002 at 23:51 UTC

    Use B::Deparse. I think it may be documented in perlopen, but the second construct adds the defined operator.

    $ perl -MO=Deparse while (<STDIN>) { print; }

    produces:

    while (defined($_ = <STDIN>)) { print $_; } - syntax OK
Re: Re: Re: Benchmarking File Retrevial
by demerphq (Chancellor) on Dec 16, 2002 at 00:06 UTC
    Well unless theres a version issue going on here then I would write that benchamrk like this
    use Benchmark 'cmpthese'; for $size (2, 200, 20_000) { $s = "." x $len; print "\nDATASIZE = $size\n"; cmpthese -5,{ chop2 => '$t = $s; chop $t; chop $t;', subs => '($t = $s) =~ s/..\Z//s;', lsubstr => '$t = $s; substr($t, -2) = "";', rsubstr => '$t = substr($s, 0, length($s)-2);', }; }
    There are a few more ways, but this is a direct but less verbose copy of what you posted. The -5 argument indicates that the benchmarking for each item should take at minimum 5 seconds. (It may take longer) If you used a positive argument then it does that many runs of the given code. It will warn if you dont use enough iterations for it to get a "reasonable" sample.

    Please consult the Benchmark documentation as I suspect the interface has moved on since the edition of Programming Perl that you are using. I say this because your code does work, but it looks like Benchmark has been updated to do that idiom automatically.

    cheers

    --- demerphq
    my friends call me, usually because I'm late....