Please explain: canvas and font commands slowing down 
Author Message
 Please explain: canvas and font commands slowing down

I have a Tcl/Tk application that makes frequent calls to "font measure
..." and "$canvas create text ...".  I am seeing *huge* variations in
the time that these commands take to complete, depending on the order
in which they are performed.  (There's an example at the end of this
post.)

Bizarrely, adding commands can make things run *much* faster.  (See
the demonstration.)  I can't see any reason for this, and I'm almost
convinced it's a bug (famous last words).  I'd really like some help
as this is making my application next to useless.

Regards,

Jeremy Henty

Details:
Tcl/Tk 8.0p2, RedHat Linux 5.1, AMD K6 233Mhz.  

Demonstration script:

#!/bin/sh

#  Restart the script in the Tcl/Tk interpreter.  \

#  Create a canvas and a font.  
canvas .canvas
pack .canvas
set the_font [font create]

#  Test procedure.  
proc test {create_text} \
{
  global the_font

  font measure $the_font ""
  if {$create_text} {.canvas create text 0 0 -font $the_font}

Quote:
}

puts [time {test 0} 50]
puts [time {test 1} 50]
puts [time {test 0} 50]
puts [time {test 1} 50]

.canvas delete all

puts [time {test 0} 50]
puts [time {test 1} 50]
puts [time {test 0} 50]
puts [time {test 1} 50]

exit

Output of the demonstration script:

145462 microseconds per iteration
5993 microseconds per iteration
47 microseconds per iteration
147 microseconds per iteration
145462 microseconds per iteration
5982 microseconds per iteration
46 microseconds per iteration
145 microseconds per iteration



Wed, 02 May 2001 03:00:00 GMT  
 Please explain: canvas and font commands slowing down
I tried your code on my windows95 system.  My results were similar.

I experimented a bit, and I think I know what's happening.

It looks like 'font measure' takes much longer to execute when there
are no Tk objects which use the font being measured.

Try running my 'do_tests'  proc on your system and see what results
you get.   I think you will find that 'test 0' is consistently faster
now.

...Mark

############################
proc setup_for_test {} {
   global the_font
   .canvas del all
   .canvas create text 0 0 -font $the_font

Quote:
}

proc do_tests {} {
   setup_for_test
   puts [time {test 0} 500]
   setup_for_test
   puts [time {test 1} 500]
   setup_for_test
   puts [time {test 0} 500]
   setup_for_test
   puts [time {test 1} 500]
Quote:
}

############################



Quote:
>I have a Tcl/Tk application that makes frequent calls to "font measure
>..." and "$canvas create text ...".  I am seeing *huge* variations in
>the time that these commands take to complete, depending on the order
>in which they are performed.  (There's an example at the end of this
>post.)

>Bizarrely, adding commands can make things run *much* faster.  (See
>the demonstration.)  I can't see any reason for this, and I'm almost
>convinced it's a bug (famous last words).  I'd really like some help
>as this is making my application next to useless.

>Regards,

>Jeremy Henty

>Details:
>Tcl/Tk 8.0p2, RedHat Linux 5.1, AMD K6 233Mhz.  

>Demonstration script:

>#!/bin/sh

>#  Restart the script in the Tcl/Tk interpreter.  \

>#  Create a canvas and a font.  
>canvas .canvas
>pack .canvas
>set the_font [font create]

>#  Test procedure.  
>proc test {create_text} \
>{
>  global the_font

>  font measure $the_font ""
>  if {$create_text} {.canvas create text 0 0 -font $the_font}
>}

>puts [time {test 0} 50]
>puts [time {test 1} 50]
>puts [time {test 0} 50]
>puts [time {test 1} 50]

>.canvas delete all

>puts [time {test 0} 50]
>puts [time {test 1} 50]
>puts [time {test 0} 50]
>puts [time {test 1} 50]

>exit

>Output of the demonstration script:

>145462 microseconds per iteration
>5993 microseconds per iteration
>47 microseconds per iteration
>147 microseconds per iteration
>145462 microseconds per iteration
>5982 microseconds per iteration
>46 microseconds per iteration
>145 microseconds per iteration



Thu, 03 May 2001 03:00:00 GMT  
 Please explain: canvas and font commands slowing down
Mark,

Thanks very much for investigating my problem.  I'm sorry it's taken
me so long to reply, but I've been on holiday for a few days.  

Quote:

> It looks like 'font measure' takes much longer to execute when there
> are no Tk objects which use the font being measured.

It does indeed!  In fact "font create text ... -font foo" is *also*
extremely slow when nothing uses font foo (there's a demonstration
of this at the end of this post).  I added your workaround (create
an empty text tag using the font before doing anything else) to my
application and it now runs at a respectable speed.  Thanks again!  

I think maybe this should be noted in an FAQ somewhere as a "Tk gotcha".  

Regards,

Jeremy Henty

Demonstration script:

#!/bin/sh

#  Restart the script in the Tcl/Tk interpreter.  \

#  Create a canvas and a font.  
canvas .canvas
pack .canvas
set the_font [font create]

#  Test procedures.  

proc test {script} \
{
  test_aux $script 0
  test_aux $script 1
  test_aux $script 0
  test_aux $script 1

Quote:
}

proc test_aux {script create_text} \
{
  global the_font

  .canvas delete all
  if {$create_text} {.canvas create text 0 0 -font $the_font}

  puts [time $script 50]

Quote:
}

#  Procedures to test.  

proc measure_font {} \
{
  global the_font

  font measure $the_font ""

Quote:
}

proc create_text {} \
{
  global the_font

  .canvas create text 0 0 -font $the_font -tags foo
  .canvas delete foo

Quote:
}

test {measure_font}
test {create_text}

exit

Output of demonstration script:

145005 microseconds per iteration
42 microseconds per iteration
147452 microseconds per iteration
42 microseconds per iteration
157220 microseconds per iteration
155 microseconds per iteration
148750 microseconds per iteration
157 microseconds per iteration



Sun, 13 May 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. : strange slow-down in canvas, interpreter, and more....

2. text widget dramatically slows down execution of unrelated commands on HP-UX

3. Slow down this turtle, please

4. Slow down this turtle, please

5. Slow down this turtle, please

6. My browser moves too fast - Please help to slow it down

7. Font commands quite slow on Linux?

8. canvas postscript command with Japanese fonts

9. patch: tcl/tk canvas slow for large canvas

10. I explain!!! Exec ever put down my computer

11. Avoiding canvas binding command after canvas object binding fires

12. vw2.5 sometimes slows down when large

 

 
Powered by phpBB® Forum Software