I think that what's happening here is that the optimizer can see that the composition you wrote out is really (lamdba (x) (+ (+ (+ x 1) 2) 3)), but it cannot see through the use of compose.
In general you can use raco decompile to look at what the optimizer has done (which is how I know it didn't figure out that this is equivalent to (lambda (x) (+ (+ x 1) 5))). I modified list.rkt to do a similar transformation (making compose a macro; sticking the code below to the end of list.rkt and renaming compose to -compose) and see only about a 3.5 slowdown. Also, if you change your program to this one: #lang racket (define (f1 x) (+ x 1)) (define (f2 x) (+ x 2)) (define (f3 x) (+ x 3)) (unless (zero? (random 1)) (set! f1 1) (set! f2 1) (set! f3 1)) (define (f* x) (f1 (f2 (f3 x)))) (define f-comp (compose f1 f2 f3)) (define-syntax-rule (test f) (time (for ([i (in-range 100000000)]) (f i)))) (test f*) (test f-comp) which also defeats the optimizer, then you'll the two versions take similar times. Robby (require (for-syntax "pre-base.rkt")) (define-syntax (compose stx) (syntax-case stx () [(_ e1 e2 ...) (with-syntax ([(x1 x2 ...) (generate-temporaries #'(e1 e2 ...))]) (with-syntax ([body (let loop ([xs (syntax->list #'(x1 x2 ...))]) (cond [(null? (cdr xs)) (with-syntax ([f (car xs)]) #'(f y))] [else (with-syntax ([f (car xs)] [rst (loop (cdr xs))]) #'(f rst))]))]) #'(let ([x1 e1] [x2 e2] ...) (if (and (eqv? 1 (procedure-arity x1)) (eqv? 1 (procedure-arity x2))...) (begin (printf "good! ~s\n" 'body) (λ (y) body)) (-compose x1 x2 ...)))))] [(_)#'(-compose)] [x #'-compose])) PS: the code just above is not really valid for compose. But it might be okay for compose1. On Mon, Sep 14, 2015 at 11:50 PM, Roman Klochkov <kalimehta...@gmail.com> wrote: > (define (f1 x) (+ x 1)) > (define (f2 x) (+ x 2)) > (define (f3 x) (+ x 3)) > > (define (f* x) (f1 (f2 (f3 x)))) > (define f-comp (compose f1 f2 f3)) > > (define-syntax-rule (test f) > (time > (for ([i (in-range 100000000)]) > (f i)))) > > (test f*) > (test f-comp) > > --- > > cpu time: 375 real time: 375 gc time: 0 > cpu time: 5109 real time: 5172 gc time: 0 > > If try > (define f1 (curry + 1)) > (define f2 (curry + 2)) > (define f3 (curry + 3)) > > then even worse: > > cpu time: 46796 real time: 46891 gc time: 5241 > cpu time: 60860 real time: 60953 gc time: 6945 > > -- > You received this message because you are subscribed to the Google Groups > "Racket Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to racket-users+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.