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: email@hidden
- Date: Sun, 05 Apr 2015 12:27:39 +0200
Hello.
> LOL! Where does "OMG" get translated into "Oh My God”?
It’s an easter egg. ;)
> As far as just writing code that is clear, that's good engineering advice since the code is usually just a means to an end. However I like to understand how things work also. Asking that question has many times led me to discovering better techniques and/or ones that I wasn't aware of. "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 agree with you, that we should always pursue answers to our questions, and necessary optimization, isn’t the same as premature optimization. If you know that something needs to be optimized in order to run, then I think that is not premature. But trying to tweak out the last millisecond from the start is. And it js like Jon Pugh said, very often nobody has a faint idea about what needs to be optimized, before the code is profiled. A good rule of thumb is to minimize the number of apple events though.
I also do believe, that it is almost 20 year ago since a case statement was faster than an if in C. With those highly advanced compilers (clang and gcc), it really doesn’t matter what kind of construct you use, it is more about the optimization flags and pragmas.
Cheers
Tommy/McUsr
5. apr. 2015 kl. 12:17 skrev S. J. Cunningham <email@hidden>:
> LOL! Where does "OMG" get translated into "Oh My God"?
>
> I overlooked "else if" because Script Debugger doesn't include it in it's pre-defined paste commands. I agree it looks a lot cleaner. and I'll be using it a lot more in the future.
>
> As far as just writing code that is clear, that's good engineering advice since the code is usually just a means to an end. However I like to understand how things work also. Asking that question has many times led me to discovering better techniques and/or ones that I wasn't aware of. "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.
>
>
> On Apr 5, 2015, at 1:44 AM, email@hidden wrote:
>
>> Jon Pugh is of course totally right. My first attempt was to make the code readable, then if you have anything special for your “segments” of use-cases, then you just comment the segments, within the nested if test.
>>
>> -- More like a case, since now, omitting an exit repat will
>> -- work like omitting a break, allowing you to adjust the value,
>> -- so you can jump through more conditions, like a case statement works in most
>> -- languages, except for java I believe
>>
>> on jumpTableFor1by13(n)
>> repeat
>> if n = 1 then
>> return 1
>> else if n = 2 then
>> return 2
>> else if n = 3 then
>> return 3
>> else if n = 4 then
>> say "OMG"
>> set n to 5
>> else if n = 5 then
>> return 5
>> else if n = 6 then
>> return 6
>> else if n = 7 then
>> return 7
>> else if n = 8 then
>> return 8
>> else if n = 9 then
>> return 9
>> else if n = 10 then
>> return 10
>> else if n = 11 then
>> return 11
>> else if n = 12 then
>> return 12
>> else if n = 13 then
>> return 13
>> else
>> return -1
>> end if
>> end repeat
>> end jumpTableFor1by13
>> 5. apr. 2015 kl. 07:34 skrev email@hidden:
>>
>>> on jumpTableFor1by13(n)
>>> if n = 1 then
>>> return 1
>>> else if n = 2 then
>>> return 2
>>> else if n = 3 then
>>> return 3
>>> else if n = 4 then
>>> return 4
>>> else if n = 5 then
>>> return 5
>>> else if n = 6 then
>>> return 6
>>> else if n = 7 then
>>> return 7
>>> else if n = 8 then
>>> return 8
>>> else if n = 9 then
>>> return 9
>>> else if n = 10 then
>>> return 10
>>> else if n = 11 then
>>> return 11
>>> else if n = 12 then
>>> return 12
>>> else if n = 13 then
>>> return 13
>>> else
>>> return -1
>>> end if
>>> end jumpTableFor1by13
>>> 5. apr. 2015 kl. 00:15 skrev S. J. Cunningham <email@hidden>:
>>>
>>>> I have a need to compare n different things and take a different branch for each one. A great application for the imaginary Applescript "case" statement :) Does anyone have a clever way to do this other than a brute force bunch of if-then statements?
>>>>
>>>> I figured that, on the average, I would be executing n/2 if-then tests for each iteration. In my case n is currently 13 so figure 6.5 tests. I decided to get clever and speed things up by doing the equivalent of a binary search, ie dividing the cases in half at each branch, thus reducing the number of tests to log base 2 of n, in my case, log base 2 of 13 or about 3.7. I expected this to perform significantly faster: 3.7 tests on average rather than 6.5.
>>>>
>>>> I wrote the attached script to test out my theory and was surprised by the results:
>>>>
>>>> Case Statement Test Results for 80,000 Tests
>>>> (in Seconds)
>>>>
>>>> straight IF If-ElseTree Binary Search IF
>>>> Run 1 37 49 58
>>>> Run 2 51 47 40
>>>> Run 3 39 40 44
>>>> Run 4 44 44 43
>>>> Run 5 44 42 41
>>>> Average 43.0 44.4 45.2
>>>>
>>>> Not only was there not much difference but the Binary Search did marginally worse.
>>>>
>>>> I would be curious to hear any explanations as to why this might be so.
>>>>
>>>> ------------- Attached Script --------------------------
>>>>
>>>> set nTests to 80000
>>>> set nRuns to 5
>>>> set savetoFile to true
>>>> set displayResults to true
>>>> set saveResultsFileName to "Case Statement Test Results.txt"
>>>> set resultsText to "Case Statement Test Results for " & nTests & " Tests" & return & " (in Seconds)" & ¬
>>>> return & return & tab & "straightIF" & tab & "IfElseTree" & tab & "BinarySearchIF" & return
>>>> set straightIFtimes to {}
>>>> set IfElseTreetimes to {}
>>>> set BinarySearchIFtimes to {}
>>>> set fatTab to " "
>>>>
>>>>
>>>> repeat with nRun from 1 to nRuns
>>>>
>>>> set startTime to current date
>>>> repeat nTests times
>>>> set theResult to StraightIF(random number from 1 to 13)
>>>> (*
>>>> if theResult > 0 then
>>>> display dialog "Case " & theResult
>>>> else
>>>> display dialog "Error. " & (n as text) & " is out of range"
>>>> end if
>>>> *)
>>>> end repeat
>>>> set endTime to current date
>>>> set straightIFtime to endTime - startTime
>>>> -- display dialog "StraightIF time for " & nTests & " tests" & tab & straightIFtime & " seconds"
>>>> set the end of straightIFtimes to straightIFtime
>>>>
>>>>
>>>> set startTime to current date
>>>> repeat nTests times
>>>> set theResult to IfElseTree(random number from 1 to 13)
>>>> (*
>>>> if theResult > 0 then
>>>> display dialog "Case " & theResult
>>>> else
>>>> display dialog "Error. " & (n as text) & " is out of range"
>>>> end if
>>>> *)
>>>> end repeat
>>>> set endTime to current date
>>>> set IfElseTreetime to endTime - startTime
>>>> -- display dialog "IfElseTree time for " & nTests & " tests" & tab & IfElseTreetime & " seconds"
>>>> set the end of IfElseTreetimes to IfElseTreetime
>>>>
>>>>
>>>> set startTime to current date
>>>> repeat nTests times
>>>> set theResult to BinarySearchIF(random number from 1 to 13)
>>>> (*
>>>> if theResult > 0 then
>>>> display dialog "Case " & theResult
>>>> else
>>>> display dialog "Error. " & (n as text) & " is out of range"
>>>> end if
>>>> *)
>>>> end repeat
>>>> set endTime to current date
>>>> set BinarySearchIFtime to endTime - startTime
>>>> -- display dialog "BinarySearchIF time for " & nTests & " tests" & tab & BinarySearchIFtime & " seconds"
>>>> set the end of BinarySearchIFtimes to BinarySearchIFtime
>>>>
>>>> (*
>>>> display dialog "Times for " & nTests & " tests:" & return & return & ¬
>>>> "StraightIF" & tab & straightIFtime & " seconds" & return & ¬
>>>> "IfElseTree" & tab & IfElseTreetime & " seconds" & return & ¬
>>>> "BinarySearchIF" & tab & BinarySearchIFtime & " seconds" & return
>>>> *)
>>>>
>>>> set resultsText to resultsText & "Run " & nRun & fatTab & straightIFtime & fatTab & fatTab & IfElseTreetime & fatTab & fatTab & BinarySearchIFtime & return
>>>> end repeat
>>>>
>>>> set StraightIFAverage to averageOf(straightIFtimes)
>>>> set IfElseTreeAverage to averageOf(IfElseTreetimes)
>>>> set BinarySearchIFAverage to averageOf(BinarySearchIFtimes)
>>>> set resultsText to resultsText & "Average " & fatTab & StraightIFAverage & fatTab & fatTab & IfElseTreeAverage & fatTab & fatTab & BinarySearchIFAverage
>>>>
>>>> if displayResults then
>>>> display dialog resultsText
>>>> end if
>>>>
>>>> if savetoFile then
>>>> tell application "Finder"
>>>> set hfsParentFolderPath to get container of (path to me)
>>>> set myFolderPath to (contents of hfsParentFolderPath) as text
>>>> end tell
>>>> set fID to open for access myFolderPath & saveResultsFileName with write permission
>>>> write resultsText to fID
>>>> close access fID
>>>> end if
>>>>
>>>>
>>>>
>>>>
>>>> on averageOf(aList)
>>>> set theSum to 0
>>>> repeat with n in aList
>>>> set theSum to theSum + n
>>>> end repeat
>>>> set theAverage to theSum / (count of aList)
>>>> return theAverage
>>>> end averageOf
>>>>
>>>>
>>>> on IfElseTree(n)
>>>> if n = 1 then
>>>> return 1
>>>> else
>>>> if n = 2 then
>>>> return 2
>>>> else
>>>> if n = 3 then
>>>> return 3
>>>> else
>>>> if n = 4 then
>>>> return 4
>>>> else
>>>> if n = 5 then
>>>> return 5
>>>> else
>>>> if n = 6 then
>>>> return 6
>>>> else
>>>> if n = 7 then
>>>> return 7
>>>> else
>>>> if n = 8 then
>>>> return 8
>>>> else
>>>> if n = 9 then
>>>> return 9
>>>> else
>>>> if n = 10 then
>>>> return 10
>>>> else
>>>> if n = 11 then
>>>> return 11
>>>> else
>>>> 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 if
>>>> end if
>>>> end if
>>>> end if
>>>> end if
>>>> end if
>>>> end if
>>>> end if
>>>> end IfElseTree
>>>>
>>>> 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
>>>>
>>>>
>>>> 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
>>>> _______________________________________________
>>>> 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
>>>
>>
>>
>> _______________________________________________
>> 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
>
>
> _______________________________________________
> 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
_______________________________________________
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