Page 1 of 1

Best practice

Posted: Tue May 25, 2021 5:03 am
by geo
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

Re: Best practice

Posted: Tue May 25, 2021 8:18 am
by FourthWorld
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".

Re: Best practice

Posted: Tue May 25, 2021 10:03 am
by jmburnod
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

Re: Best practice

Posted: Tue May 25, 2021 2:19 pm
by dunbarx
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

Re: Best practice

Posted: Tue May 25, 2021 5:00 pm
by cpuandnet
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

Re: Best practice

Posted: Tue May 25, 2021 6:55 pm
by dunbarx
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

Re: Best practice

Posted: Wed May 26, 2021 8:23 am
by geo
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