Dear Monks,

I'd like to hear your suggestions on how to improve speed in the following script (sorry if it is not so clean, but it is a dirty minimization of a big GUI project). The script mimics the insertion of a lot of data in a Tk Tablematrix with some formattings (adaptation of row hight to display all text in cell, coloring of rows, and indentation). The problem is speed and the bottleneck is the routine setHeight. Any pragmatic idea to reduce the loading time (improvements in the subrutine, different approach, etc.)?

use strict; use warnings; use Tk; use Tk::Font; use Tk::TableMatrix; #look my $BackgroundTableRow="#D0E1F9"; my $BackgroundTableHeader="red"; my $MainTableIpadx=2; my $MainTablePadx=2; #number of rows to print my $NumberRows=7000; our $arrayVar = {}; my $mw = MainWindow->new(); my $MainTable = createTABLE($mw); createTableRows($MainTable, $NumberRows); printData($MainTable, $NumberRows); MainLoop; sub createTABLE{ my $mw = shift; my $GuiFontData = $mw->Font(-family=> 'Arial', -size => 12); my ($rows,$cols) = (1, 3); foreach my $row (0..($rows-1)){$arrayVar->{"$row,0"} = "$row";} foreach my $col (0..($cols-1)){$arrayVar->{"0,$col"} = "$col";} my $MainTable = $mw->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -bg=>'white', -height => 4, -titlerows => 1, -titlecols => 0, -variable => $arrayVar, #-browsecommand => \&SelectTableRow, -coltagcommand => \&colSub, -resizeborders => "none", -wrap=>1, -multiline=>1, -relief => 'ridge', -selecttitles => 0, -selectmode => 'extended', -selecttype=>'row', -drawmode => 'slow', -scrollbars=>'e', -ipadx=>$MainTableIpadx, -padx=>$MainTablePadx, -pady=>12, -highlightthickness=>0 )->pack(-side=>'right', -expand => '1', -fill => 'both'); $MainTable->tagConfigure ("title", -bg => "$BackgroundTableHeader" +); $MainTable->tagConfigure ('leftjustify', -anchor=>'w'); $MainTable->tagConfigure ('WhiteRow', -bg => 'white'); $MainTable->tagConfigure ('ColoredRow', -bg => $BackgroundTableRow +); $MainTable->colWidth(0=> -100, 1=> -200, 2=> -200); $arrayVar->{"0,0"} = "Id"; $arrayVar->{"0,1"} = "Header 1"; $arrayVar->{"0,2"} = "Header 2"; $MainTable->configure(-font =>$GuiFontData); return $MainTable; } sub createTableRows{ my ($MainTable, $NumberRows) = @_; $MainTable->configure (-state=>'normal'); print "Creating rows in table (rows: $NumberRows)... "; $MainTable->insertRows(1, $NumberRows); $MainTable->configure (-state=>'disabled'); print "Done!\n"; } sub printData{ my ($MainTable, $NumberRows) = @_; my $IndexRow=1; #looping x times to simulate data, normally data is of different l +enghts. foreach (1..$NumberRows) { $arrayVar->{"$IndexRow,0"} = $IndexRow; foreach (1..2) { $arrayVar->{"$IndexRow,$_"} = "This is my text to be inser +ted This is my text to be inserted"; } setHeight($MainTable, $IndexRow); formattingTableRow($MainTable, $IndexRow); $IndexRow++; } $MainTable->tagRow('leftjustify',1..$NumberRows); } sub formattingTableRow{ my ($MainTable, $IndexRow)=@_; my $is_even = $IndexRow % 2 == 0; foreach (0..2){ if ($is_even == 0) { $MainTable->tagCell('WhiteRow',"$IndexRow,$_"); } else { $MainTable->tagCell('ColoredRow',"$IndexRow,$_"); } } } sub setHeight { my ($MainTable, $IndexRow) = @_; foreach my $cell (0..2) { $MainTable->activate("$IndexRow,$cell"); my $text = $MainTable->get('active'); my $font = $MainTable->Font (); my $widthdata = $MainTable->fontMeasure ($font, $text); my $widthcell = -(($MainTable->colWidth($cell))+($MainTableIpa +dx*2)+($MainTablePadx*2)+22); my $reqheight = int($widthdata / $widthcell) + 1; my $currheight = $MainTable->rowHeight($IndexRow); $MainTable->rowHeight($IndexRow, $reqheight) if ( ($cell == 0) + or ($reqheight > $currheight)); } }

In reply to Tk TableMatrix automatic rowheight speed problem by IB2017

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.