Skip to content

Instantly share code, notes, and snippets.

@bryanwoods
Created July 16, 2014 18:30
Show Gist options
  • Save bryanwoods/5314d84c77992d5428f2 to your computer and use it in GitHub Desktop.
Save bryanwoods/5314d84c77992d5428f2 to your computer and use it in GitHub Desktop.
Trying to benchmark an iterative vs recursive solution in Racket Scheme
#lang racket
(define min 0)
(define max 100000000)
(define (recursive-function n)
(if (>= n max)
n
(recursive-function (+ n 1))))
(define (iterative-function n)
(let loop ()
(set! n (+ n 1))
(when (< n max) (loop)))
n)
(pretty-display "Benchmarking the recursive function...")
(time (recursive-function min))
(pretty-display "Benchmarking the iterative function...")
(time (iterative-function min))
Benchmarking the recursive function...
cpu time: 7778 real time: 7778 gc time: 0
100000000
Benchmarking the iterative function...
cpu time: 12079 real time: 12072 gc time: 0
100000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment