Competition #1: Reversing a string
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Competition #1: Reversing a string
..........
			
			
													
					Last edited by [-hh] on Wed Aug 13, 2014 11:31 am, edited 1 time in total.
									
			
									shiftLock happens
						- 
				FourthWorld
- VIP Livecode Opensource Backer 
- Posts: 10065
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Competition #1: Reversing a string
You might see a very small performance boost changing this:
...to this:
...so the loop doesn't need to calculate len(s) each time through.
			
			
									
									Code: Select all
repeat with j=length(s) down to 1Code: Select all
put len(s) into N
repeat with j=N down to 1Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
						LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Competition #1: Reversing a string
..........
			
			
													
					Last edited by [-hh] on Wed Aug 13, 2014 1:58 pm, edited 1 time in total.
									
			
									shiftLock happens
						Re: Competition #1: Reversing a string
Using 5000 generated words... the example from the liveCode lesson that you want to use as a benchmark takes 70 millisecs...
The one below takes 29 millisecs...
Using a Mackbook Pro 2.53 Ghz, LC 6.5
			
							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 reversedCode: 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- Attachments
- 
			
		
		
				- 5000 word.zip
- (9.78 KiB) Downloaded 281 times
 
Re: Competition #1: Reversing a string
5000 words, 52 millisecs on my MacMini 2.7 Ghz, LC 6.5
Oh, this is my 5000th posting 
			
			
									
									
						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
- 
				FourthWorld
- VIP Livecode Opensource Backer 
- Posts: 10065
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Competition #1: Reversing a string
The repeat loop is an inherent part of the function.[-hh] wrote:You are optimizing the testing procedure, not the function of interest itself.
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
						LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Competition #1: Reversing a string
..........
			
			
													
					Last edited by [-hh] on Wed Aug 13, 2014 11:30 am, edited 1 time in total.
									
			
									shiftLock happens
						Re: Competition #1: Reversing a string
..........
			
			
													
					Last edited by [-hh] on Wed Aug 13, 2014 11:30 am, edited 1 time in total.
									
			
									shiftLock happens
						Re: Competition #1: Reversing a string
..........
			
			
													
					Last edited by [-hh] on Wed Aug 13, 2014 11:30 am, edited 1 time in total.
									
			
									shiftLock happens
						- 
				FourthWorld
- VIP Livecode Opensource Backer 
- Posts: 10065
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Competition #1: Reversing a string
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.[-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.
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
						LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Competition #1: Reversing a string
..........
			
			
													
					Last edited by [-hh] on Wed Aug 13, 2014 11:29 am, edited 1 time in total.
									
			
									shiftLock happens
						Re: Competition #1: Reversing a string
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 reverseStringRe: Competition #1: Reversing a string
..........
			
			
													
					Last edited by [-hh] on Wed Aug 13, 2014 11:29 am, edited 1 time in total.
									
			
									shiftLock happens
						Re: Competition #1: Reversing a string
@Hermann:  I confirm your diagnosis.  Thanks for the patience.  -- Dick
			
			
									
									
						Re: Competition #1: Reversing a string
Hi -hh,
 
 
Best
Klaus
			
			
									
									
						well, I can sleep well nevertheless[-hh] wrote:the bad news:
With my settings your function is about 8 times slower than the LCReverse.
 
 Very kind, but thanks, no Schnaps for me.[-hh] wrote:The good news:
If you like Grappa (or do you prefer Obstler?) I'll send you a bottle.
Best
Klaus
