Which one is faster ?

Frank D. Engel, Jr. fde101 at fjrhome.net
Wed May 18 12:42:24 EDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Note that something like:

switch someExpression

case someValue
...
...
end switch

in *compiled* languages tends to be faster than numerous or nested if 
statements, because the "someExpression" is evaluated once then 
considered for each case, whereas with the nested if statements it has 
to be evaluated multiple times, once for each branch of the comparison. 
  So my guess would be that an if statement would be faster for a 
one-way branch (or a simple, one-layer if-then-else, probably), whereas 
once you hit multiple statements the switch/case would grow to have 
more and more of a speed advantage as the number of branches increases.

Don't have time to really test this for Rev right now, though I'd 
imagine it would be similar to compiled languages in this way.


On May 18, 2005, at 12:27 PM, MisterX wrote:

> 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
>>
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/use-revolution
>
>
- -----------------------------------------------------------
Frank D. Engel, Jr.  <fde101 at fjrhome.net>

$ ln -s /usr/share/kjvbible /usr/manual
$ true | cat /usr/manual | grep "John 3:16"
John 3:16 For God so loved the world, that he gave his only begotten 
Son, that whosoever believeth in him should not perish, but have 
everlasting life.
$
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFCi3Bw7aqtWrR9cZoRAkQVAKCCaC9dVU29eVVXi7coX92bWBby+wCfQepL
jlUxCbQMxprhWDpP6f4iEbw=
=Ho+3
-----END PGP SIGNATURE-----



___________________________________________________________
$0 Web Hosting with up to 200MB web space, 1000 MB Transfer
10 Personalized POP and Web E-mail Accounts, and much more.
Signup at www.doteasy.com



More information about the use-livecode mailing list