I think $Q->enqueue( $Y, @colors ); is a typo - it should be $Q->enqueue( $y, @colors );.
Indeed. A typo. (Talking of which: for my $y ( 0 .. $width - 1 ) {, y is normally height not width; ditto for x/height.)
In your while loop i ran into some undefinedness which i tried to fix.
A few more details on that?
I was quite surprised that $image->setpixel( $_, $y, $colors $_ ) for 0 .. $#colors; works. Learning never stops.
You can take that a couple of stages further:
First, to further save on inter-thread comms traffic, you can coalesce those runs of same colored pixels.
After you've populate @colors, you compress them to pairs of color/run length like so:
my( $c, @lineSegs ) = 1; for my $x ( 0 .. $X - 2 ) { ++$c, next if $colors[ $x ] == $colors[ $x + 1 ]; push @lineSegs, [ $c, $colors[ $x ] ]; $c = 1; } push @lineSegs, [ $c, $colors[ - 1] ]; $Qout->enqueue( [ $y, \@lineSegs ] );
Then when it comes to draw them, instead of drawing a bunch of same colored pixels with individual calls to setpixel(), you draw lines:
while( my $line = $Qresults->dequeue ) { my( $y, $l ) = @{ $line }; for( @$l ) { my( $count, $color ) = @{ $_ }; $i->line( $x, $y, $x + $count, $y, $color ); $x += $count; }
Drawing a series of short line segments rather than individual pixels reduces the number of Perl-to-C graphics library calls; and has each call do more work.
So rather than calculating the whole line, you can calculate only half of the line; compress it to line segments, and queue that to the drawing thread.
On receipt, you the get the second half of the line by duplicating and reversing the first half:
## calculate the Mandelbrot values for half the line my @colors; for my $x ( 0 .. $halfX - 1 ) { push( @colors, rgb2n( (0)x3 ) ), next if clip( $x, $y ); my $m = mandelbrot( ( $x - $halfX ) / $halfX, ( $y - $half +Y ) / $halfY ); my $c = mapColors( $m ); push @colors, $m == 0 ? 0 : $c; } ## compress pixels to count/color pairs (line segments) my( $c, @lineSegs ) = 1; for my $x ( 0 .. $halfX - 2 ) { ++$c, next if $colors[ $x ] == $colors[ $x + 1 ]; push @lineSegs, [ $c, $colors[ $x ] ]; $c = 1; } push @lineSegs, [ $c, $colors[ - 1] ]; ## Queue back the line segments for half the line. $Qout->enqueue( [ $y, \@lineSegs ] );
Then in the drawing thread:
while( my $line = $Qresults->dequeue ) { my( $y, $l ) = @{ $line }; my $x = 0; ## for all the line segments, and those same segments ( again +in reverse order ) for( @$l, reverse @$l ) { ## extract the count and the color my( $count, $color ) = @{ $_ }; ## draw the line segment $i->line( $x, $y, $x + $count, $y, $color ); ## accumulating your current x position as you go. $x += $count; } }
Queue Size 640.627 MByte
That's a big queue; and a performance killer. You'd be better using a self-limiting queue (like this one I posted a couple of years ago), to prevent the queue growing so big. Of course, that won't work unless you actually have separate threads feeding the queue and reading from it. If you try it, start with a queue size of say 1000, and try varying it up and down to see how it affects performance. Often, you only need 100 or so for best performance.
In reply to Re^5: Threads From Hell #3: Missing Some Basic Prerequisites
by BrowserUk
in thread Threads From Hell #3: Missing Some Basic Prerequisites [Solved]
by karlgoethebier
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |