Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Last active December 18, 2015 03:39
Show Gist options
  • Save martintrojer/5719803 to your computer and use it in GitHub Desktop.
Save martintrojer/5719803 to your computer and use it in GitHub Desktop.
scheme interpreter timings
;; making Scheme an internal DSL in clojure is quite easy -- and runs at the same speed as clojure.
;; a bit un-fair to compare to the other interpreter-based solutions, but hey
;; http://martintrojer.github.io/clojure/2011/11/29/scheme-as-an-embedded-dsl-in-clojure/
user=> (use 'mtscheme.core)
user=> (define (fact x) (if (= x 0) 1 (* x (fact (- x 1)))))
user=> (time (doseq [_ (range 100)] (fact 20)))
"Elapsed time: 0.677392 msecs"
user=> (defn fact [n] (if (zero? n) 1 (* n (fact (dec n)))))
#'user/fact
user=> (time (doseq [_ (range 100)] (fact 20)))
"Elapsed time: 0.463245 msecs"
nil
;; http://martintrojer.github.io/clojure/2012/01/28/scheme-as-an-external-dsl-in-clojure/
user=> (use 'mtscheme.parser)
nil
user=> (use 'mtscheme.interpreter)
nil
user=> (def env (second (_eval (parse "(define (fact x) (if (= x 0) 1 (* x (fact (- x 1)))))") global-env)))
user=> (time (doseq [i (range 1000)] (first (_eval (parse "(fact 20)") env))))
"Elapsed time: 776.895388 msecs"
// http://martintrojer.github.io/fsharp/2011/11/02/scheming-in-f/
> #r "mtscheme.dll";;
--> Referenced '/home/trojerm/scheme-fsharp/mtscheme.dll'
> open mtscheme.parser;;
> open mtscheme.interpreter;;
> open mtscheme.helper;;
> let env = "(define (fact x) (if (= x 0) 1 (* x (fact (- x 1)))))" |> parse |> List.head |> (eval globalEnv) |> fst;;
> #time;;
> for i = 1 to 1000 do ("(fact 20)" |> parse |> List.head |> (eval env) |> snd);;
Real: 00:00:00.172, CPU: 00:00:00.212, GC gen0: 1
# https://github.com/martintrojer/scheme-python
>>> import mtscheme
>>> mts = mtscheme.MTScheme()
>>> mts.run("(define (factorial x) (if (= x 0) 1 (* x (factorial (- x 1)))))")
'nil'
>>> def doRun(n, expr):
... t = time.time()
... for i in range(n):
... mts.run(expr)
... return time.time() - t
...
>>> doRun(1000, "(factorial 10)")
// https://github.com/martintrojer/scheme-scala
scala> import System.{nanoTime => _time}
scala> def profile[R](code: => R, t: Long = _time) = (code, (_time - t) / 1000000.0)
profile: [R](code: => R, t: Long)(R, Double)
scala> import mtscheme.BuiltIn._
import mtscheme.BuiltIn._
scala> import mtscheme.Parser._
import mtscheme.Parser._
scala> import mtscheme.Interpreter._
import mtscheme.Interpreter._
scala> val (env, _) = eval(globalEnv, parse("(define (fact x) (if (= x 0) 1 (* x (fact (- x 1)))))").head)
scala> profile (for (i <- 1 to 1000) (eval(env, parse("(fact 20)").head))._2)
res79: (Unit, Double) = ((),1174.213965)
;; chicken scheme repl
> (define (fact x) (if (= x 0) 1 (* x (fact (- x 1)))))
> ,t (let loop ((i 0)) (unless (= i 100) (fact 50) (loop (+ i 1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment