Skip to content

Instantly share code, notes, and snippets.

@lizmat
Last active April 9, 2018 17:46
Show Gist options
  • Save lizmat/b33a04cf7f1ef4c5e6a19fe4660985f2 to your computer and use it in GitHub Desktop.
Save lizmat/b33a04cf7f1ef4c5e6a19fe4660985f2 to your computer and use it in GitHub Desktop.
Faster version of genetic algorithm
Original code:
perl6-BitVector,16,5.34862844
perl6-BitVector,32,6.211466
perl6-BitVector,64,7.8329269
perl6-BitVector,128,11.85552583
perl6-BitVector,256,18.9098477
Adapted code:
perl6-BitVector,16,3.3355078
perl6-BitVector,32,3.8888316
perl6-BitVector,64,5.3188300
perl6-BitVector,128,8.07596295
perl6-BitVector,256,13.6082418
Actual adapted code:
my int $len = 16;
my int $maxlen = 256;
my int $how-many =100000;
while $len <= $maxlen {
my $start = now;
loop (my int $i = 0; $i < $how-many; ++$i) {
my int $start = ($len -2 ).rand.Int;
my int $this-len = ($len-$start).rand.Int;
my int8 @chromosome1 = Bool.roll($len);
my int8 @chromosome2 = Bool.roll($len);
my int8 @x-chromosome = @chromosome2;
@chromosome2.splice($start,$this-len, @chromosome1.skip($start).head($this-len));
@chromosome1.splice($start,$this-len, @x-chromosome.skip($start).head($this-len))
}
say "perl6-BitVector,$len,",now - $start;
$len = $len*2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment