Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
geo
- Posts: 31
- Joined: Sun Aug 04, 2019 7:28 pm
Post
by geo » Tue May 25, 2021 5:03 am
Hello,
which function is better?
Code: Select all
function test
put field "test" into tTest
repeat with n = 1 to 3
put char n of tTest after tResult
end repeat
return tResult
end test
or
Code: Select all
function test1
repeat with n = 1 to 3
put char n of field "test" after tResult
end repeat
return tResult
end test1
Thank for the advice
-
FourthWorld
- VIP Livecode Opensource Backer
- Posts: 9823
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
-
Contact:
Post
by FourthWorld » Tue May 25, 2021 8:18 am
Define "better". And please define the context in which this will be used.
In most cases #2 will be faster, which I suppose may be part of " better".
But better still is to know the scenario of use, which is the roadmap to "best".
-
jmburnod
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
-
Contact:
Post
by jmburnod » Tue May 25, 2021 10:03 am
Hi Geo,
You also may use repeat for each.
As said Richard its depend the context.
You can get the speed execution of your script with a timer
Code: Select all
function test2
put the milliseconds into tDepTime
put field "test" into tTest
repeat for each char tChar in tTest
put tChar after tResult
end repeat
return the milliseconds - tDepTime & cr & tResult
end test2
Best regards
Jean-Marc
https://alternatic.ch
-
dunbarx
- VIP Livecode Opensource Backer
- Posts: 9656
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Post
by dunbarx » Tue May 25, 2021 2:19 pm
I cannot imagine that slower is ever better, unless there is an explicit reason to do so. I think the OP was really asking the following question:
"Does operating on a variable run faster than operating on a field"
And the answer is...
Right. But you may want to run Jean-Marc's test with a far larger value than "3" to really see the effect.
Craig
-
cpuandnet
- Posts: 32
- Joined: Thu Jan 17, 2019 6:43 am
Post
by cpuandnet » Tue May 25, 2021 5:00 pm
One might want to consider this method as well. I'm not sure if it will make any difference.
Code: Select all
put field "test" into tTest
put char 1 to 3 of tTest into tResult
return tResult
-
dunbarx
- VIP Livecode Opensource Backer
- Posts: 9656
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Post
by dunbarx » Tue May 25, 2021 6:55 pm
cpaundnet
Surely more compact, but the point was speed of processing, whether direct access to a field was different than access to a variable.
In a large loop, the difference is great.
Craig
-
geo
- Posts: 31
- Joined: Sun Aug 04, 2019 7:28 pm
Post
by geo » Wed May 26, 2021 8:23 am
Thanks for your inputs.
In my case speed isn't critical, since there isn't a lot of data to process.
Im just using 3 letters from a field