Médecins Sans Frontičres | Ärzte ohne Grenzen die Amika Methode   
Lesezeichen [ Info # Jobs # QR-Code # Projekte ]Mo 20 Mai 2024 18:05:16 # Grundlagen der Programmierung | Tcl Tutorial.


. Leftovers - time, unset .

[ Previous | Index | Next ]

The simplest method of making a script run faster is to buy a faster processor. Unfortunately, this isn't always an option. You may need to optimize your script to run faster. This is difficult if you can't measure the time it takes to run the portion of the script that you are trying to optimize.

The time command is the solution to this problem. Time will measure the length of time that it takes to execute a script. You can then modify the script, rerun time and see how much you improved it.

You may also need to optimize the memory used by a script, or perhaps clean up variables after a each pass through a loop. The unset command will delete a variable from the interpreters namespace.

After you've run the example, play with the size of the loop counters in timetst1 and timetst2. If you make the inner loop counter 5 or less, it may take longer to execute timetst2 than it takes for timetst1. This is because it takes time to calculate and assign the variable k, and if the inner loop is too small, then the gain in not doing the multiply inside the loop is lost in the time it takes to do the outside the loop calculation.

time script ?count?
Returns the number of milliseconds it took to execute script. If count is specified, it will run the script count times, and average the result. The time is elapsed time, not CPU time.
unset variableName1 ?variableName2 ...?
Deletes variableName from the interpreter's namespace. If variableName is an array name, then the entire array is deleted. If variableName is an element in an array, then only that element is deleted. If any of the variables to delete do not exist, then unset will return an error, and any variables after the non-existent variable will not be deleted.


. Example .

   proc timetst1 {lst} {
    set x [lsearch $lst "5000"]
    return $x

   proc timetst2 {array} {
    upvar $array a
    return $a(5000);

   # Make a long list and a large array.

   for {set i 0} {$i < 5001} {incr i} {
    set array($i) $i
    lappend list $i

   puts "Time for list search: [ time {timetst1 $list} 10]"
   puts "Time for array index: [ time {timetst2 array} 10]"

   proc existence {variable} {
    upvar $variable testVar;
    if {[info exists testVar]} {
     puts "$variable Exists"
    } else {
     puts "$variable Does Not Exist"

   set x 1
   set y 2
   for {set i 0} {$i < 5} {incr i} {
    set a($i) $i;

   puts "\ntesting unsetting a simple variable"
   ;# Confirm that x exists.
   existence x
   ;# Unset x
   puts "x has been unset"
   unset x
   ;# Confirm that x no longer exists.
   existence x

   puts "\ntesting unsetting a member of an array"
   existence a(0);
   puts "a0 has been unset"
   unset a(0);
   existence a(0);

   puts "\ntesting unsetting several members of an array, with an error"
   existence a(3);
   existence a(4);
   catch {unset a(3) a(0) a(4)}
   puts "\nAfter attempting to delete a(3), a(0) and a(4)"
   existence a(3);
   existence a(4);

   puts "\ntesting unsetting an array"
   existence a;
   puts "a has been unset"
   unset a;
   existence a;

[ Home | Top ]
[ . Previous | Index | Next . ]
Der Inhalt dieser Seite wurde am 06.11.2019 um 12.19 Uhr aktualisiert.
Navigation Seminare Magic Software Projekte Publikationen Kontakt Home
   huecker dot com * Germany | Datenschutz
© 1999, 2024 Franz-Josef Hücker. All Rights Reserved.
Send Page Print Page LinkedIn follow me on twitter RSS Feeds & Podcasts