with <rp8mrm$38d$
1...@gioia.aioe.org> gamo wrote:
*SKIP*
> __END__
>
> This short code last for hours. Make suggestions to speed up. Thank
> you.
First, I presume you understand you're churning through 2**30 (1.07e+09)
combinations here -- it will take time. Anyway, I've been reassured
(again) that: Less Perl makes it slow; Less Perl More Clever Perl
makes it even slower; Less Perl More Clever Perl More Implicit List
Contexts brings it to stall. However, checkout this beauty:
---
foo.cRPn0x.pl 2020-11-22 17:19:19.629862529 +0200
+++
foo.l0Fble.pl 2020-11-22 23:11:23.056820382 +0200
@@ -23,35 +23,32 @@
my $antes = time();
-my @a = (0) x $limit;
-my $count = 0;
-
my $MAXNPV = 0;
my $NPROYECTOS = 0;
-my @b;
my ($van, $ppto, $c, @pn);
+my( $oepa, $wabt ) = ( 0 );
-while ($count < $limit) {
+while ( $oepa < 2**30 ) {
# print reverse @a, "\n"; # reverse @a for avoid antinature
$van = $ppto = $c = 0;
for (0..$limit-1){
- $van += $a[$_] * $VAN[$_];
- $ppto += $a[$_] * $INV[$_];
- ++$c if ($a[$_] == 1);
+ $oepa & 2**$_ or next;
+ $van += $VAN[$_];
+ $ppto += $INV[$_];
+ ++$c
}
if ($ppto <= $PPTO && $van >= $MAXNPV){
- @b = @a;
@pn=();
for (0..$limit-1){
- push (@pn, $_+1) if $a[$_] == 1;
+ push (@pn, $_+1) if $oepa & 2**$_
}
$MAXNPV = $van;
$NPROYECTOS = $c;
- print "El vector ",@b," produce $MAXNPV con una inversión de $ppto ($c proyectos)\n";
+ $wabt = reverse( sprintf q|%*b|, $limit, $oepa ) =~ tr{ }{0}r;
+ print "El vector $wabt produce $MAXNPV con una inversión de $ppto ($c proyectos)",sprintf( " %7.0f",$oepa/(time-$antes+1)),"\n";
}
-
- @a = enum(@a); # next number or combination
+ $oepa++
}
print "Lista de proyectos: @pn\n";
@@ -60,23 +57,3 @@
exit 0;
-
-
-sub enum {
- my @l = @_;
- my $i = 0;
-zz:
- if ($l[$i] == 0) {
- $l[$i]=1;
- return @l;
- }else{
- $l[$i]=0;
- if (++$i > $count){
- $l[++$count]=1;
- return @l;
- }else{
- goto zz;
- }
- }
-}
-
Please excuse me avoiding mimicking yours Style Guidelines -- I can't
figure out what they are. Second, due to random data being used
measuring performance is kinda dubious. Anyway -- whooping three times
faster!
Now, about suggestions. First thing that springs is PDL. Couple of
times I was motivated but never have had time to find out how it works.
One suggestion, if you find yourself moving data to-and-fro PDL, then
abort -- it won't yield anything. Upload datas once, then read results
*once*.
The other thing would be gradient descent. I have no slightest idea how
to apply it here though.
--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom