Skip to content

Instantly share code, notes, and snippets.

@g000001
Forked from anonymous/fib.c
Last active December 27, 2015 02:09
Show Gist options
  • Save g000001/7250640 to your computer and use it in GitHub Desktop.
Save g000001/7250640 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
int fib(int n)
{
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
int main(int argc, char **argv)
{
fib(35);
return 0;
}
/*
% time ./fib
real 0m0.089s user 0m0.080s sys 0m0.000s
*/
(defun fib (n)
(declare (optimize (debug 0) (safety 0) (space 0) (speed 3))
(fixnum n))
(labels ((fib (n)
(if (< n 2)
n
(the fixnum
(+ (fib (1- n))
(fib (- n 2)))))))
(declare (ftype (function (fixnum) fixnum) fib)
(inline fib))
(fib n)))
(fib 35)
;=> 9227465
#|------------------------------------------------------------|
Evaluation took:
0.073 seconds of real time
0.072005 seconds of total run time (0.072005 user, 0.000000 system)
98.63% CPU
175,444,128 processor cycles
0 bytes consed
Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz
|------------------------------------------------------------|#
;;; ----------------
;;; w/o inline
(defun fib (n)
(declare (optimize (safety 0) (speed 3) (debug 0))
(fixnum n))
(if (< n 2)
(the fixnum n)
(the fixnum
(+ (fib (- n 1))
(fib (- n 2))))))
(fib 35)
;=> 9227465
#|------------------------------------------------------------|
Evaluation took:
0.172 seconds of real time
0.172011 seconds of total run time (0.172011 user, 0.000000 system)
100.00% CPU
412,324,533 processor cycles
0 bytes consed
Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz
|------------------------------------------------------------|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment