From af32224d5bae685a59522ffd81209ebcef696389 Mon Sep 17 00:00:00 2001 From: Sanel Zukan Date: Mon, 14 Jul 2008 10:42:25 +0000 Subject: [PATCH] Added clock and timeit functions Some musings with examples --- tools/capone/lib/common.ss | 13 +++++++++++++ tools/capone/src/bbb.ss | 38 ++++++++++++++++++++++++++++++++------ tools/capone/src/foo.ss | 2 +- tools/capone/src/sys.cpp | 11 +++++++++++ tools/capone/src/xxx.ss | 8 ++++---- 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/tools/capone/lib/common.ss b/tools/capone/lib/common.ss index 29a1984..8f3182f 100644 --- a/tools/capone/lib/common.ss +++ b/tools/capone/lib/common.ss @@ -79,3 +79,16 @@ (if (= n 0) lst (loop (- n 1) (cons n lst))))) + +;; +;; function for easier timing +;; +(define (timeit proc) + (let ((v1 0) + (v2 0)) + + (set! v1 (clock)) + (proc) + (set! v2 (clock)) + ;; 1000000 is value of CLOCKS_PER_SEC + (/ (- v2 v1) 1000000))) diff --git a/tools/capone/src/bbb.ss b/tools/capone/src/bbb.ss index 55654d6..617edbe 100644 --- a/tools/capone/src/bbb.ss +++ b/tools/capone/src/bbb.ss @@ -6,15 +6,41 @@ (let loop ((lst lst) (nls '())) (if (null? lst) - nls - (loop (cdr lst) (cons (proc (car lst)) nls)))))) + (reverse nls) + (loop (cdr lst) + (cons (proc (car lst)) nls)))))) +(define (map3 proc lst . more) + (if (null? more) + (map2 proc lst) + (let map-more ((lst lst) + (more more)) + (if (null? lst) + lst + (cons (apply proc (car lst) (map3 car more)) + (map-more (cdr lst) + (map3 cdr more))))))) + +(define v1 0) +(define v2 0) + +(set! v1 (clock)) (define l (iota 1009)) ;(define l (iota 10)) +(set! v2 (clock)) +(println "=== Pass 1: " (/ (- v2 v1) 1000000)) (println "Doing map...") -(println - (map2 - (lambda (x) (+ 1 x)) - l)) +(set! v1 (clock)) +(println (map3 (lambda (x) (+ 1 x)) l)) +(set! v2 (clock)) +(println "=== Pass 2: " (/ (- v2 v1) 1000000)) +;(println (map + l l)) + +(println "Time is: " (timeit + (lambda () + (define v1 (iota 100)) + (map + v1 v1) + ))) + diff --git a/tools/capone/src/foo.ss b/tools/capone/src/foo.ss index 91412a3..367ed7a 100644 --- a/tools/capone/src/foo.ss +++ b/tools/capone/src/foo.ss @@ -1,6 +1,6 @@ (define (foo a b) - "Retrun the sum of it's arguments" + ;;"Retrun the sum of it's arguments" (+ a b)) (display (foo 3 2)) diff --git a/tools/capone/src/sys.cpp b/tools/capone/src/sys.cpp index 53cf1eb..b379ac4 100644 --- a/tools/capone/src/sys.cpp +++ b/tools/capone/src/sys.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include extern "C" { @@ -64,6 +65,10 @@ static pointer s_setenv(scheme* sc, pointer args) { return sc->F; } +static pointer s_clock(scheme* sc, pointer args) { + return mk_real(sc, (double)clock()); +} + void register_sys_functions(scheme* sc) { sc->vptr->scheme_define( sc, @@ -76,4 +81,10 @@ void register_sys_functions(scheme* sc) { sc->global_env, sc->vptr->mk_symbol(sc, "setenv"), sc->vptr->mk_foreign_func(sc, s_setenv)); + + sc->vptr->scheme_define( + sc, + sc->global_env, + sc->vptr->mk_symbol(sc, "clock"), + sc->vptr->mk_foreign_func(sc, s_clock)); } diff --git a/tools/capone/src/xxx.ss b/tools/capone/src/xxx.ss index 180b446..f18ff39 100644 --- a/tools/capone/src/xxx.ss +++ b/tools/capone/src/xxx.ss @@ -37,10 +37,10 @@ (set! i (+ i 1)) ) ; -;(set! l (re-split "fooxxxbaz")) -;(for i in l -; (print i "\n") -;) +(set! l (re-split "fooxxxbaz")) +(for i in l + (print i "\n") +) ;(print (re-replace "-" "@this--is-foo" "

") "\n")