views:

161

answers:

2

How Do I profile my functions using DrScheme?

(require profile) 
(define (factorial n)
  (cond
    ((= n 1) 1)
    (else (* n (factorial (- n 1))))))

(profile factorial)

The above code returns

Profiling results
-----------------
  Total cpu time observed: 0ms (out of 0ms)
  Number of samples taken: 0 (once every 0ms)

====================================
                        Caller
Idx  Total    Self    Name+srcLocal%
     ms(pct)  ms(pct)   Callee
====================================
>

I tried: - (profile (factorial 100))
- (profile factorial) (factorial 100)
But it gives me the same result. What am I doing wrong?

+1  A: 

I'm not familiar with the profile module in PLT Scheme, but perhaps you have to actually call the function?

(profile (factorial 1000))
Greg Hewgill
I tried that too but it didnt work :(
kunjaan
Greg's answer is correct in the PLT case -- `profile` will execute the subexpression while sampling the stack as it runs. Note that your function will run very fast for small inputs and will therefore will not last long enough to have any sample with it -- you need to use large values for that.
Eli Barzilay
Yes you were correct. But why does (trace factorial)(time (factorial 50)) give me cpu time: 484 real time: 523 gc time: 0 but (profile (factorial 50)) has no effect?
kunjaan
+1  A: 

Have you tried cranking up N in (profile (factorial N)) until there's a noticeable pause?

(factorial 100) is the kind of thing a modern computer should be able to do in <1ms.

Just skimming the documentation makes me suspect its just a matter of factorial being too fast to easily profile for that case.

Kevin Montrose