in reply to Re: Check even numbers?
in thread Check even numbers?
I assume you are aware, (and it probably should be mentioned), that the referenced method is (I assume), a joke along the same lines as the included method of finding tomorrow's date:
sub tomorrow_date { sleep 86_400; return localtime(); }
Without all the compile-time reductions, lazy evaluation etc. that most properly functional languages benefit from, this method of determining the eveness of a number is ludicrously inefficient--6000+ times slower than the normal method:
#! perl -slw use strict; use Benchmark qw[ cmpthese ]; no warnings 'recursion'; sub odd { my $number = shift; return !even ($number); } sub even { my $number = abs shift; return 1 if $number == 0; return od +d ($number - 1); } cmpthese -5, { normal => q[ my $n; $_ & 1 and $n++ for 1 .. 1000; ], functional => q[ my $n; even( $_ ) and $n++ for 1 .. 1000; ], }; __END__ c:\test>junk s/iter functional normal functional 1.50 -- -100% normal 2.41e-004 625042% --
|
|---|