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);

  1. Copy a slice of an array element by element;

    Which can be done more efficiently by an array slice assignment.

  2. Assign to an array slice element-wise.

    Again, an array slice assignment.

  3. Copy a substr byte-wise.

    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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Help in improving Performance of WinConsole makeFullBackBuffer by BrowserUk
in thread Help in improving Performance of WinConsole makeFullBackBuffer by NateTut

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.