Re: The Case of the Missing Case Statement
Re: The Case of the Missing Case Statement
- Subject: Re: The Case of the Missing Case Statement
- From: "S. J. Cunningham" <email@hidden>
- Date: Wed, 29 Apr 2015 13:39:06 -0400
Once n >13, the straight function has to go through 13 tests every time while the binary search will only have to go through 4. Kind of an unfair fight :) That's why I used a random number. However, as others have pointed out, the overhead of generating the random number may mask any timing differences. Never the less, I think the binary search is more elegant ;)
On Apr 29, 2015, at 8:02 AM, Chris Page wrote:
>
>> On Apr 5, 2015, at 4:57 AM, Shane Stanley <email@hidden> wrote:
>>
>> On 5 Apr 2015, at 8:17 pm, S. J. Cunningham <email@hidden> wrote:
>>>
>>> "Just don't worry about it" is not a very satisfying answer to why executing 296K if-then statements takes longer than executing 520K of them.
>>
>> I tried your code a couple of times, and I get the results the other way round, but with similar slim margins.
>
> In my test, where I'm only timing the lookup functions with nothing else, the binary search function is taking less than half the time (5 vs 2 seconds):
>
>
> on BinarySearchIF(n)
> if n < 6 then -- 1-6
> if n < 4 then -- 1-3
> if n = 1 then
> return 1
> else
> if n = 2 then
> return 2
> else
> return 3
> end if
> end if
> else -- 4-6
> if n = 4 then
> return 4
> else
> if n = 5 then
> return 5
> else
> return 6
> end if
> end if
> end if
> else -- 7-13
> if n < 10 then -- 7-9
> if n = 7 then
> return 7
> else
> if n = 8 then
> return 8
> else
> return 9
> end if
> end if
> else -- 10-13
> if n < 12 then --10 or 11
> if n = 10 then
> return 10
> else
> return 11
> end if
> else -- 12 or 13
> if n = 12 then
> return 12
> else
> if n = 13 then
> return 13
> else
> return -1
> end if
> end if
> end if
> end if
> end if
> end BinarySearchIF
>
> on StraightIF(n)
> if n = 1 then return 1
> if n = 2 then return 2
> if n = 3 then return 3
> if n = 4 then return 4
> if n = 5 then return 5
> if n = 6 then return 6
> if n = 7 then return 7
> if n = 8 then return 8
> if n = 9 then return 9
> if n = 10 then return 10
> if n = 11 then return 11
> if n = 12 then return 12
> if n = 13 then return 13
> if n > 13 then return -1
> end StraightIF
>
>
> set startTime to current date
>
> repeat with n from 1 to 1000000
> StraightIF(n)
> end repeat
>
> set endTime to current date
> set elapsedTime1 to endTime - startTime
>
>
> set startTime to current date
>
> repeat with n from 1 to 1000000
> BinarySearchIF(n)
> end repeat
>
> set endTime to current date
> set elapsedTime2 to endTime - startTime
>
>
> {elapsedTime1, elapsedTime2}
>
> --
> Chris Page
> The other, other AppleScript Chris
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden