Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Christian888:

I haven't analyzed your code in any depth (time constraints, lack of sleep, too many interactions with global variables). But it does appear that you're not taking advantage of some of the advantages of perl to get things going a bit faster.

A couple random notes:

  • Upon a cursory review, it appears like you do quite a few unnecessary array copies. It may help if you try passing them around by reference instead of by value. For example:
    $population = regenrate_population(); ... sub regenrate_population { my $new_population = []; for my $i ( 0 .. ( $population_size - 1 ) ) { my $chromosome1 = get_nonrandom_chromosome(); my $chromosome2 = get_nonrandom_chromosome(); $$new_population[$i] = get_child( $chromosome1, $chromosome2 ) +; } return $new_population; }
  • You do a bunch of looping instead of taking advantage of list operations, like array slices. Consider the difference between:
    my $crossover_point = int( rand($chromosome_size) ); for my $i ( 0 .. ( $chromosome_size - 1 ) ) { if ( $i < $crossover_point ) { push( @$new_chromosome, @$chromosome1[$i] ); } else { push( @$new_chromosome, @$chromosome2[$i] ); } }
    and:
    my $crossover_point = int( rand($chromosome_size) ); $new_chromosome = [ @{$chromosome1}[ 0 .. $crossover_point-1], @{$chromosome2}[ $crossover_point .. $#{$chromosome2} ] ];
  • You can take advantage of the malleability of perls data structures to prevent recalculations, too, but I'm having trouble thinking of a good way to say it. I'll try again in the morning, if time allows. (Short form: rather than store population as a list of scalars (chromosomes), store the population as a list of array references, [ chromosome, fitness score, etc ]. Then when you call get_population_fitness_score, you can populate the fitness score slot, and not recompute it again until the next pass.
  • I don't know that anyone claims that perl is all that fast, anyway. We don't use it because the final product is all that fast, but because it lets us get the final product quickly. That said, I rarely find the speed of the final product to be noticeable for my purposes.
  • Finally, if you're concerned about speed, then you should definitely start playing with Benchmark so you can tell the difference in speed between various code constructs, and with a profiler so you can find out which bits of your program are consuming all the time. (I think it's Devel::Profile, but I'm not certain, as I rarely profile my code.)

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Perl slower than java by roboticus
in thread Perl slower than java by Christian888

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-18 03:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found