Which one is faster ?

MisterX b.xavier at internet.lu
Wed May 18 12:27:22 EDT 2005


i guess that if the if are single equal operations, they should be the same.

If you include functions or complex or multiple operations in a case or
[else] if, it will be tested until a false is found or maybe they have a
magic value cache referer to parse it faster but improbable. 

In any case, use the if if you have few conditions. Use case with many
options (like a menupick handler). 

Speed... OK, in terms of speed, if you do a zillion loops, these are going
to have an impact. For the ocasional if or case, it's pointless to ask but
for hardworking loops, it does matter a lot. 

In terms of design, the if will be faster because it's easier to segment the
conditional usage of conditions more precisely than with a case. However, a
case allows the same kind of usage

switch  -- note the lack of parameter, now the case handles if cases!
  case a=b
  break
end switch

Now, you can test this easily and this is the MonsieurX's quality bonus
trick of the day to do some easy benchmarking... Note it uses dynamic script
execution but im not sure it will really impact the whole test. In any case,
you can rescript it into on bigger script unwrapping it all and see for
yourself if the "average exectution time" is different... 

Could be interesting... BUT i got a another problem right now that prevents
me from doing it at all now (just farging had to fargin happen! My mom is
home a day too early! )$(@*$%@$(*%)

See the mail i wrote to Eric C. yesterday for his network benchmark?

Here's the newer version ;) Untested, might have a bug but sorry, mom is
here ()(%*)@#(*#@$))

on mouseUp
  put random(100) into a
  put random(100) into b
  hilite me
  ask "Loops:" with 10000
  if it is not a number or it < 0 then
    exit mouseup
  end if
  put it into loopcount

  -- do a standard test for non-conditional operations
  -- aka "normalizing" the data without those two statement's
  -- time taken into account into the final stats
  put dobenchtest("get a>b"&CR&"get the time", loopcount ) into normDelta
  put dobenchtest("if a>b then get the time", loopcount) into iftest
  put dobenchtest("switch"&CR&"case a>b"&cr&"get the time"&"end switch",
loopcount) into switchtext

put normDelta
put iftest
put switchtest

/*
you can script the rest regarding deltas of if and switch tests against
normdelta (item 1 of iftest-item 1 of normdelta)
which makes a normalized statistic result on the if and case information
only without the get the time or a<b operation's time taken into account.
Also note that without a minimum of 100 loops , it's not exactly meaningfull
information.
*/
unhilite me
end mouseUp

function dobenchtest what, loopcount
  do what -- cache
  put the milliseconds into tStart
  repeat loopcount times
    do what     
  end repeat
  put the milliseconds - tStart & comma after tlist
  return average(tlist), min(tlist), max(tlist), standardDeviation(tlist)
end dobenchtest

Exeptionally, Im releasing a statistics "free" open source stack about
statistics. I haven't finished the about and the total testing but it used
to work great....

Just a few minutes to make it TAOO presentable and i'll announce it.

Cheers
Xavier

> -----Original Message-----
> From: use-revolution-bounces at lists.runrev.com 
> [mailto:use-revolution-bounces at lists.runrev.com] On Behalf Of jbv
> Sent: Wednesday, May 18, 2005 17:26
> To: How to use Revolution
> Subject: Which one is faster ?
> 
> Hi list,
> 
> Which option is faster : switch staements or nested if-then-else ?
> 
> Has anyone run tests on this ?
> 
> Thanks,
> JB
> 
> 
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/use-revolution
> 



More information about the use-livecode mailing list