Page 1 of 1

'write-buffer' speed increase

Posted: Wed Oct 06, 2004 7:16 am
by HPW
Fantastic!

Code: Select all

newLISP v.8.2.1 Copyright (c) 2004 Lutz Mueller. All rights reserved.

> (set 'str "")
""
> (time(dotimes (x 10000) (set 'str (string str "hello"))))
30614
> (set 'str "")
""
> (time(dotimes (x 10000) (set 'str (append str "hello"))))
301
> (set 'str "")
""
> (time(dotimes (x 10000) (write-buffer  str "hello")))
10
> 
> (set 'str "")
""
> (time(dotimes (x 10000) (write-buffer  str  (string "hello"))))
60
> (set 'str "")
""
> (time(dotimes (x 10000) (write-buffer  str  (string "hello" "test1"))))
90
> 
> 
This code show some speed, but then I changed my demo app with tower of Hanoi, where I have a timer for newlisp-execution time
I get a speed improvment from ca.940ms to 15ms. That's from waiting a second to immediately. Whow!

Excellent for script-generating!

Posted: Wed Oct 06, 2004 12:48 pm
by Lutz
Before I would push all strings on a list than reverser and join, which was faster than append, but still kind of clumsy.

The new way with 'write-buffer', will come in handy in a lot of programming areas like report writing, script generation etc..

Lutz

ps: write-line will not have it

Posted: Wed Oct 06, 2004 2:00 pm
by eddier
Oh but if I could append many strings with it.

I use the following a bunch

Code: Select all

(append "<table>" "<tr>" "<td>" .... )
What if

Code: Select all

(write-buffer str ""<table>" "<tr>" "<td>" .... )
Eddie

Posted: Wed Oct 06, 2004 2:05 pm
by HPW
I use:

(write-buffer str (string "<table>" "<tr>" "<td>" .... ))

Posted: Wed Oct 06, 2004 2:29 pm
by Lutz
If all strings to be written are already available in a list then 'append', 'join' or 'string' is fine, but if not then 'write-buffer' with string device is better.

Also note that 'string' is much slower then 'append', because it also converts from non-string to string, while append assumes, that all args are strings.

Also when you use string on a known type, i.e.:

(string lastname "," age)

where you know that age will always be a number than it is 3 times faster to do:

(format "%s,%d" age)

Lutz

Posted: Wed Oct 06, 2004 3:18 pm
by HPW
The whole seems to be a good addition for the 'Tips and Tricks' section!

New topic: Fast string manipulation