Re: Arbitrarily Many Nested Loops
- From: "David Formosa (aka ? the Platypus)" <dformosa@xxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 30 Mar 2006 14:24:48 GMT
On 30 Mar 2006 01:32:11 -0800, Jacob JKW <jacobcdf@xxxxxxxxx> wrote:
David Formosa (aka ? the Platypus) wrote:
[...]
[...]I quickly worked out one way to do this, no guarties as to effeceny.
sub closefor (&$@) {
my $sub = shift;
my $range = shift;
return sub {
for my $i (0..$range){
$sub->($i,@_)
}
}
}
sub mulitloop (&@) {
my $sub = shift;
for (@_) {
my $oldsub = $sub;
$sub = closefor {$oldsub->(@_)} $_;
}
$sub->();
}
mulitloop {
my $sum = 0;
my $product = 1;
for (my $i; $i<=@#_; $i++) {
$sum += $_[$i];
$product *= $f_raa->[$i]->[$_[$i]];
}
$prob_ra->[$sum] += $product;
} @$n_ra;
You know as clever and elegant as this method is, it actually runs
significantly than my original ugly cut-and-paste style,
Yeah after thinking about it the above code does some things
redundently and actually causes perl to do alot of extra needless
work. This should be a little more effecent.
sub calc_prob_ra ($$$;@) {
my $sum = shift;
my $prod = shift;
my $depth = shift;
if (@_) {
my $n = shift;
calc_prob_ra($sum+$n,$prod * $f_aa->[$depth]->[$_],$depth+1,@_)
for (0..$n);
} else {
$prob_ra->[$sum] += $prod;
}
}
calc_prob_ra(0,1,1,@$n_ra);
There should be signifigently less overhead in this case (I've
eleminated a needless loop, and all the closure creation and
derefrencing).
[...]
Anyway, I'm going to keep this code for further use and some later
date, but I eventyually went with John Krahn eval method posted
above.
I should bench test Mr Krahn's eval method vs my new method. But Its
to late and I'm far to tired.
--
Please excuse my spelling as I suffer from agraphia. See
http://dformosa.zeta.org.au/~dformosa/Spelling.html to find out more.
Free the Memes.
.
- References:
- Arbitrarily Many Nested Loops
- From: Jacob JKW
- Re: Arbitrarily Many Nested Loops
- From: David Formosa (aka ? the Platypus)
- Re: Arbitrarily Many Nested Loops
- From: Jacob JKW
- Arbitrarily Many Nested Loops
- Prev by Date: Converting codepages to UTF8
- Next by Date: Re: Where to put Settings-Variables?
- Previous by thread: Re: Arbitrarily Many Nested Loops
- Next by thread: Re: Arbitrarily Many Nested Loops
- Index(es):
Relevant Pages
|