Added clock and timeit functions

Some musings with examples
This commit is contained in:
Sanel Zukan 2008-07-14 10:42:25 +00:00
parent 5a251cf812
commit af32224d5b
5 changed files with 61 additions and 11 deletions

View File

@ -79,3 +79,16 @@
(if (= n 0) (if (= n 0)
lst lst
(loop (- n 1) (cons n 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)))

View File

@ -6,15 +6,41 @@
(let loop ((lst lst) (let loop ((lst lst)
(nls '())) (nls '()))
(if (null? lst) (if (null? lst)
nls (reverse nls)
(loop (cdr lst) (cons (proc (car lst)) 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 1009))
;(define l (iota 10)) ;(define l (iota 10))
(set! v2 (clock))
(println "=== Pass 1: " (/ (- v2 v1) 1000000))
(println "Doing map...") (println "Doing map...")
(println (set! v1 (clock))
(map2 (println (map3 (lambda (x) (+ 1 x)) l))
(lambda (x) (+ 1 x)) (set! v2 (clock))
l)) (println "=== Pass 2: " (/ (- v2 v1) 1000000))
;(println (map + l l))
(println "Time is: " (timeit
(lambda ()
(define v1 (iota 100))
(map + v1 v1)
)))

View File

@ -1,6 +1,6 @@
(define (foo a b) (define (foo a b)
"Retrun the sum of it's arguments" ;;"Retrun the sum of it's arguments"
(+ a b)) (+ a b))
(display (foo 3 2)) (display (foo 3 2))

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <edelib/Missing.h> #include <edelib/Missing.h>
extern "C" { extern "C" {
@ -64,6 +65,10 @@ static pointer s_setenv(scheme* sc, pointer args) {
return sc->F; return sc->F;
} }
static pointer s_clock(scheme* sc, pointer args) {
return mk_real(sc, (double)clock());
}
void register_sys_functions(scheme* sc) { void register_sys_functions(scheme* sc) {
sc->vptr->scheme_define( sc->vptr->scheme_define(
sc, sc,
@ -76,4 +81,10 @@ void register_sys_functions(scheme* sc) {
sc->global_env, sc->global_env,
sc->vptr->mk_symbol(sc, "setenv"), sc->vptr->mk_symbol(sc, "setenv"),
sc->vptr->mk_foreign_func(sc, s_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));
} }

View File

@ -37,10 +37,10 @@
(set! i (+ i 1)) (set! i (+ i 1))
) )
; ;
;(set! l (re-split "</" "<a href=\"foo\">foo</a>xxx<a href=\"baz\">baz</a>")) (set! l (re-split "</" "<a href=\"foo\">foo</a>xxx<a href=\"baz\">baz</a>"))
;(for i in l (for i in l
; (print i "\n") (print i "\n")
;) )
;(print (re-replace "-" "@this--is-foo" "<p>") "\n") ;(print (re-replace "-" "@this--is-foo" "<p>") "\n")