in reply to Re: RFC: Text::Grap
in thread RFC: Text::Grap
I'm not really convinced you're testing what you think you're testing. That's because you're attempting to close on the lexical start/middle/end variables, but the string is being eval'd way outside of that scope. That's why, when possible, I prefer to see anonymous code refs instead. So I tried converting your code to that - and, while I was at it, I made a minor optimisation to grap where I didn't copy all the elements to a lexical variable first ("grap2"). And then another minor optimisation to stop copying anything ("grap3").
#! /usr/bin/perl use warnings; use strict; use List::Util 'first'; use Benchmark qw(:hireswallclock cmpthese); sub grap (&@) { my ($sub,@list) = @_; foreach (@list) { return 1 if ($sub->($_)); } return; } sub grap2 (&@) { my $sub = shift; foreach (@_) { return 1 if ($sub->($_)); } return; } sub grap3 (&\@) { my $sub = shift; foreach (@$_[0]) { return 1 if ($sub->($_)); } return; } my $count = 10000; my @data = ( [ start => [ (0) x $count ] ], [ middle => [ (0) x $count ] ], [ end => [ (0) x $count ] ], ); $data[0][1][0] = 1; $data[1][1][$count/2 - 1] = 1; $data[2][1][-1] = 1; foreach my $i (@data) { my $name = $i->[0]; my $array = $i->[1]; print uc "\n$name:\n"; cmpthese (-1, #2_000_000, { 'grap' => sub { my $result = grap { $_ } @$array } +, 'grap2' => sub { my $result = grap2 { $_ } @$array } +, 'grap3' => sub { my $result = grap2 { $_ } @$array } +, 'grep' => sub { my $result = grep { $_ } @$array } +, 'first' => sub { my $result = first { $_ } @$array } +, }) }
Which tells me that the second optimisation I made (grap3 vs grap2) isn't much of one ;-)START: Rate grap grep grap2 grap3 first grap 3397/s -- -37% -93% -93% -99% grep 5381/s 58% -- -89% -89% -99% grap2 47287/s 1292% 779% -- -3% -92% grap3 48596/s 1330% 803% 3% -- -92% first 580886/s 16998% 10695% 1128% 1095% -- MIDDLE: Rate grap grap3 grap2 first grep grap 1152/s -- -22% -23% -75% -79% grap3 1486/s 29% -- -0% -67% -72% grap2 1493/s 30% 0% -- -67% -72% first 4567/s 297% 207% 206% -- -15% grep 5381/s 367% 262% 260% 18% -- END: Rate grap grap3 grap2 first grep grap 756/s -- -15% -16% -69% -86% grap3 891/s 18% -- -1% -63% -84% grap2 898/s 19% 1% -- -63% -84% first 2400/s 218% 170% 167% -- -56% grep 5493/s 627% 517% 512% 129% --
However, these are much lower numbers for rates which tells me that they are looping each time. I have a really fast machine already - I don't see how you could loop through 10,000 elements via grep, and do that 2.3 million times per second - that's 23 billion function calls per second, way past believability in any language on a single CPU.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: RFC: Text::Grap
by Ieronim (Friar) on Jul 13, 2006 at 17:54 UTC | |
|
Re^3: RFC: Text::Grap
by Ieronim (Friar) on Jul 13, 2006 at 17:36 UTC |