The compiler inlines the call to `aux` in `fib2` because that call is readily apparent. It's not so apparent in the other cases that the function `aux` is always called.
At Wed, 27 Apr 2016 06:42:03 -0700 (PDT), Jerry Jackson wrote: > Hello all, > > I was experimenting a bit yesterday and discovered something that surprised > me. Here are two fibonacci functions: > > (define fib1 > (letrec ([aux (lambda (i n) > (if (< n 2) > 1 > (+ (fib1 i (- n 2)) (fib1 i (- n 1)))))]) > (let ([funs (vector aux aux)]) > (lambda (index num) > ((if (= index 0) aux (vector-ref funs index)) index num))))) > > (define fib2 > (letrec ([aux (lambda (i n) > (if (< n 2) > 1 > (+ (fib2 i (- n 2)) (fib2 i (- n 1)))))]) > (let ([funs (vector aux aux)]) > (lambda (index num) > (if (= index 0) > (aux index num) > ((vector-ref funs index) index num)))))) > > I expected them to behave basically identically (in fact, I thought they > would > probably generate the same code). However, that was not the case: > > > (time (fib1 0 40)) > cpu time: 4490 real time: 4489 gc time: 0 > 165580141 > > (time (fib1 1 40)) > cpu time: 5031 real time: 5027 gc time: 0 > 165580141 > > (time (fib2 0 40)) > cpu time: 3042 real time: 3040 gc time: 0 > 165580141 > > (time (fib2 1 40)) > cpu time: 5031 real time: 5027 gc time: 0 > 165580141 > > (time (fib1 0 40)) > cpu time: 4535 real time: 4532 gc time: 0 > 165580141 > > (time (fib2 0 40)) > cpu time: 3027 real time: 3025 gc time: 0 > 165580141 > > > > It looks like one of the functions is 1.5 times faster than the other (in the > i == 0 case). Any ideas as to why? > > Thanks, > --Jerry Jackson > > -- > 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 [email protected]. > 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 [email protected]. For more options, visit https://groups.google.com/d/optout.

