Assigning variables

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
timlit
Posts: 9
Joined: Tue Feb 22, 2011 12:15 am

Assigning variables

Post by timlit » Thu Oct 18, 2012 6:40 pm

I have a tip for those annoyed by the LC way of assigning variables.
[I didn't find the issue addressed on the forum - prior to posting]
So, my solution is in using custom functions (FileMaker-like fashion):
let a, 1 ("put 1 into a")
leta a, 1 ("put 1 after a")
letb a, 1 ("put 1 before a")

Though the wrappers are slower than "put ... into" and "put ... after/before" ,
they are still quite useful in non-critical GUI scripts. {passage edited after the helpful comment below}

If you find this usefull, I'll be happy to learn of it. :)

Code: Select all

//************************************//
//<put this into a button script>//
on mouseUp
   runtest
end mouseUp


on runtest
   local tStart, tFinish
   let tStart, the millisec
   local res=0
   repeat for 10000 times
      //put 1+res after res
      let res, res+1
   end repeat
   let tFinish, the millisec - tStart
   answer tFinish
end runtest


on let @par, val //1.1x faster than  pure code below:
   put val into par
end let

on leta @par, val//2.1x slower than  pure code below:
   put val after par
end leta

on letb @par, val//2.2x slower than  pure code below:
   put val before par
end letb
// end of script //
//****************//
Last edited by timlit on Thu Oct 18, 2012 7:20 pm, edited 1 time in total.
Tim

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Assigning variables

Post by mwieder » Thu Oct 18, 2012 6:56 pm

Sorry, that's a nonsensical benchmark.
There's no way that a wrapper around a chunk of code can be faster than the code it's executing.

Try this instead. You'll see that your "let" function is actually 20 times slower than the native code.

Code: Select all

on runtest
   local tStart, tFinish, tFinish2, tFinish3
   let tStart, the millisec
   local res=0
   repeat for 10000 times
      //put 1+res after res
      let res, res+1
   end repeat
   let tFinish, the millisec - tStart
   
   put the millisec into tStart
   repeat for 10000 times
      //put 1+res after res
      put res+1 into res
   end repeat
   let tFinish2, the millisec - tStart
   
   put the millisec into tStart
   repeat for 10000 times
      //put 1+res after res
      add 1 to res
   end repeat
   let tFinish3, the millisec - tStart
   
   answer tFinish, tFinish2, tFinish3
end runtest

timlit
Posts: 9
Joined: Tue Feb 22, 2011 12:15 am

Re: Assigning variables

Post by timlit » Thu Oct 18, 2012 7:07 pm

Good point. Yes, poor test on my part.
What I'd expect, though is of the wrapper to be slower at all times.
Yet, if the tasks are not critical back-end jobs, I still prefer to use my custom function.

I simply do not like the "put-shmut" thing.

:? :) Thanks.
Tim

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Assigning variables

Post by mwieder » Thu Oct 18, 2012 7:20 pm

No problem - if there's no speed penalty then you should write code you that you can read later on
And I give you extra credit points for declaring your variables, too. :P

I implemented a preprocessor in glx2 (http://bitbucket.org/mwieder/glx2) that allows you to say

Code: Select all

x = 3
instead of

Code: Select all

put 3 into x
and it mostly works, at least for simple transformations. But of course you can't go back and try to work with it in the IDE's script editor any more after that.

timlit
Posts: 9
Joined: Tue Feb 22, 2011 12:15 am

Re: Assigning variables

Post by timlit » Thu Oct 18, 2012 7:40 pm

As for declaring vars, I come from compiled languages and well know the importance.

Thank you, mwieder, for mentioning your GLX2, I'm thrilled.
If all goes well, no way will I return to its LC counterpart.
Tim

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Assigning variables

Post by mwieder » Thu Oct 18, 2012 7:50 pm

Thanks, but glx2 is an open source project. I'm just the maintainer.

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Assigning variables

Post by snm » Thu Oct 18, 2012 8:55 pm

I prefer to use one syntax not thinking if I need speed or not
Marek

Post Reply