http://qs321.pair.com?node_id=576881

enhering has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I'm building a data aquisition system in Perl that colects data at the rate of 10 cicles per second, and I'd like to show that in a graph on a Tk window.

If I use Tk::Canvas and create boxes or dots for each aquired data point, soon I'll fill all the memory because, if I understood correctly, each of those points is an entity on Tk.

Using Imager with Tk::photo was tried too, but the convertion from Imager format to PNG, as stated on Tk::Photo example, using base64 takes too long.

GD was also tried, but we need more functionality for the generated graphics.

In fact, what I'm looking for is a suggestion of a way to draw primitives on a canvas that are not items or objects, as was done in the past on VGA screens. The Tk::Photo + Imager solution would be fine if the conversion did not take so long.

Can you suggest me a way of solving this problem?


The following piece of code examples the problem. In a athlon64 notebook it takes 70% of the CPU to run.
#!/usr/bin/perl use strict; use Tk; use Tk::Photo; use MIME::Base64; use Tk::PNG; use Imager; my $xsize = 200; my $ysize = 200; my $cicle; my ($x, $y) = (int($xsize/2), int($ysize/2)); my $image = Imager->new(xsize=>$xsize, ysize=>$ysize); my $blue = Imager::Color->new( 255, 255, 255 ); my $image_data; $image->write(data =>\$image_data, type=>'png') or die "Cannot save image: ", $image->errstr; $image_data = encode_base64($image_data); my $main = MainWindow->new; my $tk_image = $main->Photo(-data => $image_data); my $screen = $main->Label(-image=>$tk_image)->pack; my $id = $main->repeat(100,\&move); MainLoop; sub move { my ($dx, $dy); my $i = int(rand(100)); if (($i/5) == int($i/5)) { $dx = 0; } elsif (($i/2) == int($i/2)) { $dx = 1; } else { $dx = -1; } my $j = int(rand(100)); if (($j/5) == int($j/5)) { $dy = 0; } elsif (($j/2) == int($j/2)) { $dy = 1; } else { $dy = -1; } if (($x + $dx) > $xsize) { $x = $xsize; } elsif (($x + $dx) < 0) { $x = 0; } else { $x += $dx; } if (($y + $dy) > $ysize) { $y = $ysize; } elsif (($y + $dy) < 0) { $y = 0; } else { $y += $dy; } $image->setpixel(x=>$x, y=>$y, color=>$blue); $cicle++; print "Cicle $cicle. Coords: $x, $y. Diference $dx, $dy.\n"; $image->write(data =>\$image_data, type=>'png') or die "Cannot save image: ", $image->errstr; $image_data = encode_base64($image_data); $tk_image->configure(-data => $image_data); $screen->configure(-image=>$tk_image); }