Which one is faster ?

Mark Wieder mwieder at ahsoftware.net
Wed May 18 12:48:51 EDT 2005


Jonathan-

Wednesday, May 18, 2005, 8:28:21 AM, you wrote:

LJ> I ran a test on 500,000 records, testing between a single if-then vs. a
LJ> single switch, and I found that the if-then was faster. It was
LJ> noticeably faster, but not hugely faster. I do not remember the exact
LJ> difference - sorry.

Interesting. I found that counter-intuitive, so I checked things out
with 4W RevBench (thanks, Richard). I find that the switch statement
consistently runs some quite a bit faster on four tests. Obviously the
value of x here will be the major factor in determining the total run
time in the nested-if case.

-- test 1:
put 4 into x
switch x
 case 1
  put x into y
  break
 case 2
  put x into y
  break
 case 3
  put x into y
  break
 case 4
  put x into y
  break
 default
  put x into y
end switch

-- test2:
put 5 into x
if x = 1 then
  put x into y
else
  if x = 2 then
   put x into y
  else
   if x = 3 then
    put x into y
   else
    if x = 4 then
     put x into y
    else
     put x into y
    end if
   end if
  end if
end if

value of x:    % faster (100k runs):
1               0
2               15
3               25
4               35
5               35

-- 
-Mark Wieder
 mwieder at ahsoftware.net



More information about the use-livecode mailing list