Code: Select all
(define (gen-wrapper:gen-wrapper x)
(letn (obj-sym (sym (string "genobj#" (inc gen-wrapper:i)))
obj-val (sym obj-sym obj-sym))
(when x (set obj-val x))
(context obj-sym)
)
)
I did some benchmarks with it though and was surprised to find, that even for very large lists, this did not seem to improve performance much:
Code: Select all
(define (how-many-0s lst , (num-zeros 0) break)
(dolist (item lst break)
(if (zero? item)
(inc num-zeros)
(setf break true)
)
)
(push num-zeros lst)
)
(define-macro (eval-print what)
(println what " => " (eval what))
)
(setf obj '(0 0 0 1 2 5 3)) ; 22.376 vs 1943.697
; (setf obj (append (dup 0 50 true) (dup 1 10000 true))) ; 5043.505 vs 5306.893
; (setf obj (append (dup 0 50 true) (dup 1 20000 true))) ; 9808.213 vs 8523.749
(eval-print (time (dotimes (_ 10000) (how-many-0s obj))))
(eval-print (setf obj (gen-wrapper obj)))
(eval-print (time (dotimes (_ 10000) (how-many-0s obj))))
As you can see even with extremely large lists of numbers (10050 elements), the direct value passing was faster. Only when 20050 elements were being passed did it seem to matter. And for small lists the performance was much much worse for the wrapper.
So I have three questions:
1) Why is this? I'm assuming it's because accessing the default function in 'dolist' still copies the list?
2) Can this be improved? I.e. Can 'dolist' or whatever it is reference the list instead of copying it?
3) Can we get a library function that takes a symbol and returns the context that symbol belongs to? Take a look at my gen-wrapper function, I end up having to "hack" this through the use of the 'context' function which is normally used to change contexts, and only because newLISP can't do that within a function call can I get away with it. I've found myself wishing for a dedicated function to retrieve the context of a symbol many times.