in reply to Help in improving Performance of WinConsole makeFullBackBuffer
This is untested! You'll need to check it carefully.
First thing to look at is the inner loop. And the first thing I noticed there is that these lines are loop invariant.
$destRow = $current->{'rowTop'} + $row - 2; if ($destRow>$screen->{'height'}) { $destRow = $screen->{'height'}; }
(Ie They do not vary with the value of the inner loop counter), so they should be moved outside.
Next, these three lines respectively:
$backAtt[$destRow][$destCol] = $current->{'backAtt'}[$row - 1][$col - 1]; $self->{'zBuffer'}[$destRow][$destCol] = $active; substr($backTxt[$destRow], $destCol, 1) = substr($current->{'backTxt'}[$row - 1],$col - 1, 1);
Which can be done more efficiently by an array slice assignment.
Again, an array slice assignment.
Can be replaced by a single substring assigment.
For that to happen, you need to calculate the source and destination start points and the width.
Putting that together I get:
sub makeFullBackBuffer { my ($self) = @_; my (@backAtt, @backTxt, $screen, $current, $destCol, $destRow, $ac +tive); $screen = $self->{miniwin}[ 0 ]; for( 1 .. $screen->{height} ) { push @backTxt, ' ' x $screen->{width}; push @backAtt, [ (0) x $screen->{width} ]; } for $active (reverse @{ $self->{winStack} } ) { $current = $self->{miniwin}[ $active ]; for my $row ( 1 .. $current->{height} ) { $destRow = $current->{rowTop} + $row - 2; $destRow = $screen->{height} if $destRow>$screen->{height} +; my $colMin = $current->{colTop} - 1; my $colMax = min( $screen->{width}, $current->{colTop} + $screen->{width} - 2 ); my $cols = $colMax - $colMin +1; @{ $backAtt[ $destRow ] }[ $colMin .. $colMax ] + = @{ $current->{backAtt}[ $row - 1 ] }[ 0 .. cols ] +; @{ $self->{zBuffer}[$destRow] }[ $colMin .. $colMax ] = ( $active ) x $cols; substr( $backTxt[ $destRow ], $colMin, $cols ) = substr( $current->{backTxt}[ $row - 1 ], 0, $cols ); } } return \( @backAtt, @backTxt ); }
Looking at that, I see that you could simplify the $destRow also, but as-is--assuming my logic is not too screwed, you could (maybe) see an order of magnitude improvement through the elimination of the inner loop.
Update:One thing you might consider is that Win32::Console gives access to the console apis and they allow the attachment of mutliple buffers to a screen which can be switched very quickly. The api also provides primatives for copying rectangular windows of both text and attributes (and introspection), which would be far more efficient than the role-your-own equivalents in the module you're using--but the switchover would probably involve a considerable amount of work.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Help in improving Performance of WinConsole makeFullBackBuffer
by NateTut (Deacon) on Apr 28, 2009 at 14:15 UTC | |
|
Re^2: Help in improving Performance of WinConsole makeFullBackBuffer
by NateTut (Deacon) on Apr 28, 2009 at 19:31 UTC | |
by BrowserUk (Patriarch) on Apr 28, 2009 at 19:36 UTC | |
by NateTut (Deacon) on Apr 30, 2009 at 14:40 UTC |