Hallo,
I was wondering if the use of optional arguments conses or is otherwise slow and should be avoided in performace-critical code?
Cheers,
Afficher les réponses par date
On 2010-02-10, at 11:57 AM, Alex Queiroz wrote:
Hallo,
I was wondering if the use of optional arguments conses or is
otherwise slow and should be avoided in performace-critical code?
Gambit's parameter handling including optional and keyword parameters does not cons (except when there is a rest parameter). Optional parameters are handled faster than keyword parameters. Here's a test which may shed some light on the time each kind of parameter handling takes. This was executed on a 2.8 GHz intel core 2 duo. As you can see, optional parameters are handled reasonably quickly (comparable to a required parameter).
Marc
(declare (standard-bindings) (block) (fixnum) (not safe) (not inline) )
(define (f a) (if a a a)) (define (g a #!optional (b 11) (c 22)) (if a b c)) (define (h a #!optional (b 11) #!key (c 22)) (if a b c))
(define (loop0 n) (if (> n 0) (begin (f #t) (loop0 (- n 1))))) (define (loop1 n) (if (> n 0) (begin (g #t) (loop1 (- n 1))))) (define (loop2 n) (if (> n 0) (begin (g #t 1) (loop2 (- n 1))))) (define (loop3 n) (if (> n 0) (begin (g #t 1 2) (loop3 (- n 1))))) (define (loop4 n) (if (> n 0) (begin (h #t 1 c: 2) (loop4 (- n 1)))))
(time (loop0 100000000)) (time (loop1 100000000)) (time (loop2 100000000)) (time (loop3 100000000)) (time (loop4 100000000))
; results: ; ;(time (loop0 100000000)) ; 1053 ms real time ; 1052 ms cpu time (1051 user, 1 system) ; no collections ; no bytes allocated ; 1 minor fault ; no major faults ;(time (loop1 100000000)) ; 1049 ms real time ; 1047 ms cpu time (1046 user, 1 system) ; no collections ; no bytes allocated ; no minor faults ; no major faults ;(time (loop2 100000000)) ; 1253 ms real time ; 1253 ms cpu time (1251 user, 2 system) ; no collections ; no bytes allocated ; no minor faults ; no major faults ;(time (loop3 100000000)) ; 1351 ms real time ; 1351 ms cpu time (1349 user, 2 system) ; no collections ; no bytes allocated ; no minor faults ; no major faults ;(time (loop4 100000000)) ; 4471 ms real time ; 4467 ms cpu time (4462 user, 5 system) ; no collections ; no bytes allocated ; no minor faults ; no major faults