The Case of the Missing Case Statement
The Case of the Missing Case Statement
- Subject: The Case of the Missing Case Statement
- From: "S. J. Cunningham" <email@hidden>
- Date: Sat, 04 Apr 2015 18:15:56 -0400
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