[gambit-list] Profiling scheme + C programs

Phil Dawes pdawes at users.sf.net
Fri Jun 15 04:54:53 EDT 2007

Hi Gambit List,

I've got a gambit program which includes some C code for searching
memory mapped files and I was wondering how best to profile it. I've
used statprof, but since it works using gambit interrupts it doesn't
statistically highlight time spent in functions containing C code.

I've been using a 'define-timed' macro I wrote (see below) which records
the accumulated wall-time a function uses. This is fine, but it means
you have to instrument each function manually (by changing '(define
(foo) ..) to '(define-timed (foo) ..)' which is a bit sucky.

Could this be modified to instrument each function automatically? (I
tried redefining 'define' but couldn't get this to work cleanly)
Is there a better way?

Many thanks,



(define *timerhash* (make-table))

(define (reset-timer) (set! *timerhash* (make-table)))

(define (accum-time name thunk)
   (let* ((timebefore (real-time))
          (res (thunk)))
     (table-set! *timerhash* name (+ (table-ref *timerhash* name 0)
                                     (- (real-time) timebefore)))

(define (get-times)
   (map (lambda (e)
          (set! total (+ total (cdr e)))
          (cons (car e) (* 1000 (cdr e))))
        (sort-list (table->list *timerhash*)
                   (lambda (a b) (> (cdr a)
                                    (cdr b))))))

(define-syntax define-timed
   (syntax-rules ()
     ((define-timed (name . args) body ...)
      (define (name . args)
        (accum-time 'name (lambda ()
                            body ...)))



(define-timed (myfunction)
    (do stuff))

;...do stuff

More information about the Gambit-list mailing list