in reply to Re^5: rough approximation to pattern matching using local (Multi Subs)
in thread rough approximation to pattern matching using local
Okay. I'm assuming that '^n' means '0 to n-1'. Despite that it doesn't explain the presence of the '^' in the line: my $iterations = ^10 ** $_;; which seems to me (from the displayed effect of the statement), to be exactly the same as: my $iterations = 10 ** $_;? (Ie. no caret.)
Anyway, making my assumption, and ignoring the anomaly, what your benchmark seems to show is that alternately calling two non-multi subs, 1/2 a million times each, takes 1/6 the time required to call one of two multisubs determined by pattern matching 1/2 a million times each.
Is the cost of the convenience of runtime functional (argument) pattern matching worth the benefit of not having to decide which function to call explicitly?
In my world: when 10 minutes becomes an hour; or an hour becomes six hours; or 6 hours becomes 36? Absolutely not!
I suspect that in Haskell; ML; OCaml; Erlang; Miranda; Clean; -- Ie. compiled functional languages -- the runtime cost is minimal because the compilers can, if not completely infer the matching function at compile-time; at least substantially reduce the possibilities through type-inference.
But (again; just my suspicion), P6 has to in(tro)spect the argument list at runtime and then attempt a best match (or fail?) against the signatured possibilities for the given function name, at runtime. Hence the performance cost.
Update:For reference: This Perl 5.10.1 chosing between two functions and executing each of them 1/2 million times:
#! perl -slw use strict; use Time::HiRes qw[ time ]; sub a{} sub b{} my $start = time; $_ % 2 ? a() : b() for 1 .. 1e6; printf "%.9f\n", time() - $start; __END__ C:\test>junk99 0.275810003
That's 16 times faster than your a() | b() code; and 88 times faster than the P6 multi-sub version.
|
|---|