OK, old thread, but here is my entry:
;;using lambda function - no apparent speed advantage to named function call
(defun string->bin_strings (str / )
(mapcar '(lambda (str / mask out)
(setq out "" mask 128)
(repeat 8
(if (zerop (logand arg mask))
(setq out (strcat out "0"))
(setq out (strcat out "1"))
)
(setq mask (/ mask 2))
)
out
)
(vl-string->list str))
)
After I got past this garbage:
;;naive iterative version - 8 bits - a real dog
(defun byte->bin_string (arg / n out power2)
(setq n 8 out "")
(repeat 8
(setq n (1- n)
power2 (expt 2 n))
(if (>= arg power2)
(progn
(setq out (strcat out "1")
arg (- arg power2))
)
(setq out (strcat out "0"))
)
)
out
)
I tried 3 ways of doing basically the same thing:
;;iterative - using logand from high bit - much faster than first one (no surprise)
(defun int->bin_string-l (arg / mask out)
(setq out "" mask 128)
(repeat 8
(if (zerop (logand arg mask))
(setq out (strcat out "0"))
(setq out (strcat out "1"))
)
(setq mask (/ mask 2))
)
out
)
;;iterative - using logand from low bit - slightly slower than above!
(defun int->bin_string-r (arg / mask out)
(setq out "" mask 1)
(repeat 8
(if (zerop (logand arg mask))
(setq out (strcat "0" out))
(setq out (strcat "1" out))
)
(setq mask (* mask 2))
)
out
)
;;iterative - using logand & lsh from low bit - slower than using multiply!
(defun int->bin_string-lsh (arg / mask out)
(setq out "" mask 1)
(repeat 8
(if (zerop (logand arg mask))
(setq out (strcat "0" out))
(setq out (strcat "1" out))
)
(setq mask (lsh mask 1))
)
out
)
Test results using BenchRRB by Robt. Bell:
Note: -> results not 100% repeatable - I am not sophisticated enough to know why
So, I suggest running the same test multiple times (like, 5 or 6)
(This could be a whole 'nother topic)
Command: (bench '(int->bin_string-l int->bin_string-r int->bin_string-lsh)
(_> '(255) 1000)
INT->BIN_STRING-L
Elapsed: 15
Average: 0.0150
INT->BIN_STRING-R
Elapsed: 16
Average: 0.0160
INT->BIN_STRING-LSH
Elapsed: 32
Average: 0.0320
Test of posted complete functions:
;;;timing using BenchRRB <- not 100% consistent - must run multiple times
Command: (bench '(_strtobin eea-str2bin stringtobinary-tw stringtobinary-gile
('(_> string->bin_strings) '("ABCD") 1000)
_STRTOBIN - MP
Elapsed: 110
Average: 0.1100
EEA-STR2BIN
Elapsed: 79
Average: 0.0790
STRINGTOBINARY-TW
Elapsed: 78
Average: 0.0780
STRINGTOBINARY-GILE - 1st Post
Elapsed: 94
Average: 0.0940
STRING->BIN_STRINGS
Elapsed: 78
Average: 0.0780
@ MP:
I think (expt) is slowing yours down
Yes, I see that part of your intention was to compare language solutions, so probably you could write faster LISP if you intended to only do that. Now, I am determined to explore some .NET - it is time.
@ Gile:
I didn't test your subsequent LISP post
@ TW:
Your entry is far from "cheesy"
@ Vovka:
Sorry, I kept getting a "cannot reenter LISP" error when I tried to wrap your nice recursion, so I had to omit yours.
Please accept my apology for not being able to find my error.
@ Evgeny:
Impressive. Concise & fast.
You da man.
Comments:
I would naively expect (lsh) to beat * or /. However, the results I got here agree with my past experience, that (lsh) is not as fast as one might expect, given its definition. I conclude it is implemented in a way not very close to the hardware.