Re: Slow switch?

From: miguel sofer (msofer_at_users.sf.net)
Date: 10/16/03


Date: Wed, 15 Oct 2003 19:29:21 -0300

Luciano ES wrote:
> I made a script:
>
> if { this } {that }
> if { this } {that }
> if { this } {that }
> if { this } {that }
> ...
>
> After a couple of days, I looked at the code again, felt ridiculous and
> replaced the whole series with a switch statement. Suddenly, the script
> became slower. It is very short so I can't really say it takes longer to
> do its job once it is launched, but it does seem to take longer to launch.
>
> Logic tells me I am crazy, but I kept the two scripts, ran them several
> times and still think the one with 'switch' takes longer *to launch*
> then the one with lots of IFs. Is there an unquestionable way to measure
> how long an app takes to launch?
>
> Thank you.
>

Don't trust logic in performance matters ;)

It may indeed be quite slower, on at least one of two counts:

(a) switch always compares as strings; this may be slower if your
conditions are numerical comparisons, depending on other details

(b) if this is inside the body of a proc (which should be true if you
are worried about performance), [if] is much faster as it is
bytecompiled, while [switch] isn't ([switch] is now bcc'ed in HEAD,
soon-to-be tcl8.5a1)

Until that comes around, the fastest is to use
   if {...} {
      ....
   } elseif {...} {
      ....
   }

If the code runs more than once or accesses the same variable
repeatedly, put it within a proc body for extra gains.

Miguel



Relevant Pages

  • Re: always run as administrator
    ... if you launch the app from a script. ... even if the script has been obfuscated. ... the /savecred switch is the way to go. ... Using /savecred the first time a user launches the app the pwd ...
    (microsoft.public.windowsxp.security_admin)
  • Re: Set default browser and email programs
    ... Kmenu | KDE Components | Component Chooser, and you can select your default ... script to launch it if you want to run multiple instances. ... switch accordingly. ...
    (alt.os.linux.suse)
  • Script to open word remotely
    ... try psexec from sysinternals.com with the -i switch to ... launch the program interactively - this could be easily ... >I am trying to run a script across a network to run word ... >I can start a process on the remote machine, ...
    (microsoft.public.scripting.vbscript)
  • Re: Processing workload distribution
    ... The Perl we're using is the 64 bit build of 5.8.8 from Activestate. ... I created a script that uses threads to launch a series of standalone ...
    (comp.lang.perl.misc)
  • Re: Webster van Robot help
    ... Install Microsoft Script Debugger or Visual studio. ... Use a switch statement. ... the robot does in fact move. ...
    (comp.lang.javascript)