Well, I've never used Lisp, but I did stay at a Holiday Inn last night read Higher-Order Perl, so I'll take that as a compliment. | [reply] [Watch: Dir/Any] |
My perl smells like a beginner’s fear of writing a code that he will not be able to read again in six months time :-) Hence I did everything as explicitly as possible, including all parentheses. I was not aware that being explicit would slow the code in perl. Is this correct?
BTW, the slowest sub seems to be “get_phenotype” I think it take up to 25 milliseconds on my machine, which is a lot compared to the rest of the code. Can you see anything that might be causing the slowdown in that sub?
| [reply] [Watch: Dir/Any] |
I saw no real issues with your code.
Style-wise, @$chromosome1[$i] is needless use of an array slice, $$chromosome1[$i] and $chromosome1->[$i] are more appropriate. (The latter is usually considered more readable, but they are equivalent.)
Also, for my $i ( 0 .. ( $population_size - 1 ) ) { ...; $new_population[$i] = ...; } could be simplified to for ( 1 .. $population_size ) ) { ...; push @new_population, ...; }
Performance-wise, there's some array copying that could probably be avoid. Some can definitely be avoided by returning an array instead of a reference to an array. I don't know if it'll amount to much time.
Also, you're recompiling /\d/ and /\D/ repeatedly. Perhaps you should use qr/\d/ and qr/\D/ instead of '\d' and '\D'. This won't amount to much, I suspect.
There's surely tricks you can use to speed up your code, but that's not to say you did anything wrong.
Perl is typeless. That will make it slower in general. You might want to take a peak at PDL for this project.
| [reply] [Watch: Dir/Any] [d/l] [select] |
The odd extra bracket isn't the point - it's the maximal iterations in your algorithm that is the issue. Although the suggestion about using one liners inline does save a fair amount. But a really well-conceived algorithm ought to reduce iterations by several orders of magnitude. labelled loops with winning exits might be a start. Also, sorting data once to reduce iterating through is better than always iterating the lot. Even with rand, you could generate blocks of data in advance and sort them, rather than pick up values on the fly.
| [reply] [Watch: Dir/Any] |