Welcome to MSDN Blogs Sign in | Join | Help

Quiz: String performance optimization

This code takes .03 seconds to run on my computer Try running it on yours.

 

ns=SECONDS()

x=""

FOR i = 1 TO 30000

      x=x+"OneTwoThree"

ENDFOR

?SECONDS()-ns

?LEN(x)

 

 

Modified slightly, it takes 15 seconds. That's 500 times slower!

 

ns=SECONDS()

x=""

FOR i = 1 TO 30000

      x="OneTwoThree"+x

ENDFOR

?SECONDS()-ns

?LEN(x)

 

I

Why the difference?

 

Published Wednesday, September 28, 2005 11:43 AM by Calvin_Hsia
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Quiz: String performance optimization

Wednesday, September 28, 2005 3:18 PM by Steven
I would guess that the first method could expose an optimisation that uses an efficient memory reallocation scheme, as C's realloc or C++'s std::vector could, thus growing the string as it needs to, with only handful of memory allocations (assuming the allocated memory doubles in size when it needs to grow).

In the second case, a temporary string object is probably created that has the appropriate size, this is then placed in x, with the original x being discarded. This would result in 30000 allocations and 30000 deallocations.

# re: Quiz: String performance optimization

Wednesday, September 28, 2005 3:30 PM by tony
darn somebody beat me to it!!

# re: Quiz: String performance optimization

Wednesday, September 28, 2005 3:46 PM by Stuart Ballard
Even if the second method is doing a realloc also (which it ought to be, if it's smart enough to do it in the first case), the second method has to be O(n^2) because the original string has to be shifted to the right, which means copying n*len("OneTwoThree") chars of data n times.

The first case can leave the existing data in place and stick the new data on the end.

# re: Quiz: String performance optimization

Wednesday, September 28, 2005 4:35 PM by Travis Owens
Great little micro optimization post, I would have never thought of this. I'll have to try this out in C# and see if the results are similar.

# re: Quiz: String performance optimization

Thursday, September 29, 2005 7:46 AM by Frank Camp
Hi Travis,

Markus Egger and Rod Paddock wrote an article about the string performances in VFP and C#:

http://www.eps-cs.com/VFPConversion/Article.aspx?quickid=030054

# re: Quiz: String performance optimization

Thursday, September 29, 2005 2:31 PM by Fabio Lunardon
however the optimization
should be removed when
there is a passage for reference

CLEAR

x=REPLICATE('1',100)

X= x + CSTUFF(@x)

? x && 'null'

x=REPLICATE('1',101)

X= x + CSTUFF(@x)

? x && '4444'


PROCEDURE cstuff(sss)
SSS = '4444'
RETURN NULL

# String Optimization. How does it work?

Thursday, September 27, 2007 8:35 PM by Calvin Hsia's WebLog

In this post: Quiz: String performance optimization , I showed some very similar code with drastically

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker