Competition #1: Reversing a string

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Competition #1: Reversing a string

Post by [-hh] » Thu Dec 05, 2013 8:22 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:31 am, edited 1 time in total.
shiftLock happens

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Competition #1: Reversing a string

Post by FourthWorld » Thu Dec 05, 2013 9:43 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Competition #1: Reversing a string

Post by [-hh] » Thu Dec 05, 2013 9:57 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 1:58 pm, edited 1 time in total.
shiftLock happens

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Competition #1: Reversing a string

Post by Dixie » Thu Dec 05, 2013 9:58 pm

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
Attachments
5000 word.zip
(9.78 KiB) Downloaded 251 times

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Competition #1: Reversing a string

Post by Klaus » Thu Dec 05, 2013 10:20 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Competition #1: Reversing a string

Post by FourthWorld » Thu Dec 05, 2013 10:41 pm

[-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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Competition #1: Reversing a string

Post by [-hh] » Thu Dec 05, 2013 10:48 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:30 am, edited 1 time in total.
shiftLock happens

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Competition #1: Reversing a string

Post by [-hh] » Thu Dec 05, 2013 11:03 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:30 am, edited 1 time in total.
shiftLock happens

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Competition #1: Reversing a string

Post by [-hh] » Thu Dec 05, 2013 11:18 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:30 am, edited 1 time in total.
shiftLock happens

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Competition #1: Reversing a string

Post by FourthWorld » Thu Dec 05, 2013 11:57 pm

[-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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Competition #1: Reversing a string

Post by [-hh] » Fri Dec 06, 2013 1:22 am

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:29 am, edited 1 time in total.
shiftLock happens

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: Competition #1: Reversing a string

Post by rkriesel » Fri Dec 06, 2013 7:36 am

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Competition #1: Reversing a string

Post by [-hh] » Fri Dec 06, 2013 9:30 am

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:29 am, edited 1 time in total.
shiftLock happens

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: Competition #1: Reversing a string

Post by rkriesel » Fri Dec 06, 2013 10:48 am

@Hermann: I confirm your diagnosis. Thanks for the patience. -- Dick

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Competition #1: Reversing a string

Post by Klaus » Fri Dec 06, 2013 1:02 pm

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

Post Reply