Page 1 of 2

Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 8:22 pm
by [-hh]
..........

Re: Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 9:43 pm
by FourthWorld
You might see a very small performance boost changing this:

Code: Select all

repeat with j=length(s) down to 1
...to this:

Code: Select all

put len(s) into N
repeat with j=N down to 1
...so the loop doesn't need to calculate len(s) each time through.

Re: Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 9:57 pm
by [-hh]
..........

Re: Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 9:58 pm
by Dixie
Using 5000 generated words... the example from the liveCode lesson that you want to use as a benchmark takes 70 millisecs...

Code: Select all

function reversed pString   
   local tReversedString    
   repeat with tChar = the length of pString down to 1      
      put char tChar of pString after tReversedString  
   end repeat 
   return tReversedString
end reversed
The one below takes 29 millisecs...

Code: Select all

function reverseString theString
   put the number of chars of theString into charCount
   repeat for each char thischar in theString
      put char charCount of theString after backwardsString
      subtract 1 from charCount
   end repeat
   return backwardsString
end reverseString
Using a Mackbook Pro 2.53 Ghz, LC 6.5

Re: Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 10:20 pm
by Klaus
5000 words, 52 millisecs on my MacMini 2.7 Ghz, LC 6.5

Code: Select all

function reversed pString
   put 1 into tCounter
   repeat for each char tChar in pString
      put tChar into tArray[tCounter]
      add 1 to tCounter
   end repeat
   put the keys of tArray into tKeys
   sort lines of tKeys numeric descending
   
   repeat for each line tLine in tKeys
      put tArray[tLine] after reversedtext
   end repeat
   return reversedtext
end reversed
Oh, this is my 5000th posting :D

Re: Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 10:41 pm
by FourthWorld
[-hh] wrote:You are optimizing the testing procedure, not the function of interest itself.
The repeat loop is an inherent part of the function.

Dixie's function goes even further in refining the loop, using the "for each" option which greatly reduces the things LiveCode must expect may change during each iteration.

If options to reduce things that may change within the loop are prohibited then I don't understand the nature of the exercise.

Re: Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 10:48 pm
by [-hh]
..........

Re: Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 11:03 pm
by [-hh]
..........

Re: Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 11:18 pm
by [-hh]
..........

Re: Competition #1: Reversing a string

Posted: Thu Dec 05, 2013 11:57 pm
by FourthWorld
[-hh] wrote:is it possible, that you don't have the same conditions for the test of the functions? For example with reading from disk or memory.
Whether s originated from a file read into memory, or is loaded from the contents of a field, or is created ad hoc via script, it would still be a variable in memory. At the point the function is called it shouldn't matter where it originated.

If the exercise is to perform this operation on data too large to fit into memory, then like most things in computing a very different algorithm would be needed than one optimized for working on in-memory data.

Re: Competition #1: Reversing a string

Posted: Fri Dec 06, 2013 1:22 am
by [-hh]
..........

Re: Competition #1: Reversing a string

Posted: Fri Dec 06, 2013 7:36 am
by rkriesel
Here's a version that takes 6.4 milliseconds for the given 5000 words on a 2012 iMac 3.2GHz Core i5 with OS X 10.9 and LiveCode 6.1.3 in script debug mode.

Code: Select all

function reverseString pString
    local tString
    repeat with tIndex = number of chars in pString down to 1
        put char tIndex of pString after tString
    end repeat
    return tString
end reverseString

Re: Competition #1: Reversing a string

Posted: Fri Dec 06, 2013 9:30 am
by [-hh]
..........

Re: Competition #1: Reversing a string

Posted: Fri Dec 06, 2013 10:48 am
by rkriesel
@Hermann: I confirm your diagnosis. Thanks for the patience. -- Dick

Re: Competition #1: Reversing a string

Posted: Fri Dec 06, 2013 1:02 pm
by Klaus
Hi -hh,
[-hh] wrote:the bad news:
With my settings your function is about 8 times slower than the LCReverse.
well, I can sleep well nevertheless :D
[-hh] wrote:The good news:
If you like Grappa (or do you prefer Obstler?) I'll send you a bottle.
Very kind, but thanks, no Schnaps for me.


Best

Klaus