Code: Select all
SWAP a, b
Regards
Moderator: Klaus
Code: Select all
SWAP a, b
Code: Select all
put a into b
put b into a
Code: Select all
put a into temp
put b into a
put temp into b
Code: Select all
swap a, b
Code: Select all
swap a, b
Code: Select all
on mouseUp
put random(5) into theTest
if theTest = 1 then
put "1" && theTest
else if theTest = 2 then
put "2" && theTest
else if theTest = 3 then
put "3" && theTest
else if theTest = 4 then
put "4" && thetest
else
put "5" && theTest
end if
end mouseUp
ukimiku wrote:Real-world application are typically specialized sorting routines where elements get "swapped" (the contents of a becomes the contents of b and vice versa, but you cannot write someting likesince as soon as the interpreter executes the first line, the original contents of b is overwritten. The typical solution is to use a temporary variable to hold one of the contents before the swap takes place, e.g.Code: Select all
put a into b put b into a
which is needlessly cumbersome. How about only writingCode: Select all
put a into temp put b into a put temp into b
and you're done.Code: Select all
swap a, b
In my case, I wrote an implementation of the Blowfish algorithm. In it, many contents exchanges take place, and the above method (which is called very often, so it takes up a significant amount of time, too) makes the code not only slower and more cumbersome, but also harder to read than justOther languages have it too. Programming without SWAPI find equally unnverving as programming nested conditionals without ELSEIF.Code: Select all
swap a, b
Hoping to shed some light on the swappin' business...
Regards,
Code: Select all
on swap @pA, @pB
get pA
put pB into pA
put it into pB
end swap
Code: Select all
local tA,tB
on mouseUp
put 4 into tA
put 5 into tB
swap tA,tB
put tA && tB
end mouseUp
on swap theA,theB
put theA into tB
put theB into tA
end swap
FourthWorld wrote:Code: Select all
on swap @pA, @pB get pA put pB into pA put it into pB end swap
sturgis wrote:Or something like this? Yours is more generic though, so I think it would be the way to go.Code: Select all
local tA,tB on mouseUp put 4 into tA put 5 into tB swap tA,tB put tA && tB end mouseUp on swap theA,theB put theA into tB put theB into tA end swap
FourthWorld wrote:Code: Select all
on swap @pA, @pB get pA put pB into pA put it into pB end swap
Only the age of the programmer.ukimiku wrote:Could you please explain to me the difference between a handler that starts with "on" and a "command" construct?
Both should benchmark identically, but if your testing shows a performance difference I'd be very interested to know.In my mental model of Runtime, if your implementation of SWAP was used in a stack, a message would be added to the pending messages and then processed by the "on swap" handler. Is that correct? If so, would it be much slower than using a "command swap" construct?
I have a long list of things I'd like added to the engine - I post half of them to the RQCC just in case Mark Waddingham finds himself with too much time on his hands.Of course, one canwrite such a handler that references object pointers (I find your use of "it" pretty elegant), and I could have written something alike myself, but an in-built SWAP would still be my preference, if only for reasons of execution speed. Also, I wouldn't have to remind myself for each project to copy the SWAP code into my stack.
Hi Mike, that's not quite accurate.ukimiku wrote:while recognizing that "on" handlers do not process pending messages as sent with the "SEND IN..." (time) command.
I believe, was meant to refer to your original questionThe "pending messages" is a queue for message sent with the "send in <time>" command, so not really the best descriptor here.
In other words, issuing a command (while it does follow the same rules as message handlers) is not conceptually the same (but only "conceptually") as adding a message to the "pending messages". It's a very subtle point, and probably not worth worrying about too much, perhaps when considering adding a command as "private" (which means that only the current script location (eg in a button, or a given card) will have access to the command, rather than it being available via the message path to be called from other places). Just to reiterate, if you put the following handler in the stack script:a message would be added to the pending messages and then processed by the "on swap" handler.
Code: Select all
on myHandler
answer "This dialog was brought to you through processing a send in time construct"
end myHandler
Code: Select all
on mouseUp
send "myHandler" to this stack in 2000 milliseconds
end mouseUp
and change it to a private command like inon swap @pA, @pB
get pA
put pB into pA
put it into pB
end swap
Code: Select all
private command swap @pA, @pB
get pA
put pB into pA
put it into pB
end swap
Code: Select all
local tA, tB
on mouseUp
-- Many tasks are so quick they don't show a measurable
-- difference unless they're run multiple times, so we
-- run through the number of iterations specified here:
put 1000000 into tIterations
--
-- TEST 1:
put the millisecs into t
repeat tIterations
swapA tA, tB
end repeat
put the millisecs - t into t1
--
-- TEST 2:
put the millisecs into t
repeat tIterations
swapB tA, tB
end repeat
put the millisecs - t into t2
-- TEST 3:
put the millisecs into t
repeat tIterations
swapC tA, tB
end repeat
put the millisecs - t into t3
--
-- DISPLAY RESULTS:
put t1 && t2 && t3
end mouseUp
private command swapA @pA, @pB
get pA
put pB into pA
put it into pB
end swapA
command swapB @pA, @pB
get pA
put pB into pA
put it into pB
end swapB
on swapC
get pA
put pB into pA
put it into pB
end swapC