6

Did some googling ("racket profiling", "racket measure performance"), but didn't find any and there are no examples in the docs. Even did google "profile" search on htdp - no luck. (profile (f ...)) output isn't that obvious other than for small snippets.

Ideally I want something like python's python -m cProfile usage examples.

1
  • 4
    Some people might want to close this as a tool/library request, but since so much of the Racket development environment is bundled together, this is a really a question about "how do I use racket's profiler?" Commented Jun 3, 2014 at 19:51

1 Answer 1

14

When I search for "Python profiling", both DuckDuckGo and Google give this as the top result: 26.4. The Python Profilers.

(Although I scanned it quickly, it seems like more of a reference than a "thorough usage guide" with examples. So if you had something else in mind, perhaps you could please link to that?)

The equivalent Racket documentation would be: Profile: Statistical Profiler.

A usage example:

#lang racket
(require profile)
(profile-thunk (thunk (function-to-profile arg0 arg1) ))

Here (thunk e) is just a convenience for (lambda () e).


Bigger example:

#lang racket

(module mod racket
  (provide f)
  (define (f)
    (for/list ([i 10000])
      i)))

(require (prefix-in mod: 'mod))

(define (f)
  (for ([i 10000])
    (mod:f)))

(require profile)
(profile-thunk f)

For me this outputs:

Profiling results
-----------------
  Total cpu time observed: 5666ms (out of 5753ms)
  Number of samples taken: 105 (once every 54ms)

========================================================
                               Caller
Idx   Total        Self      Name+src             Local%
      ms(pct)      ms(pct)     Callee
========================================================
[1] 5666(100.0%)    0(0.0%)  [running body] /tmp/profile.rkt:##f
                               profile-thunk14 [2]100.0%
--------------------------------------------------------
                               [running body] [1] 100.0%
[2] 5666(100.0%)    0(0.0%)  profile-thunk14 ...e-pkgs/profile-lib/main.rkt:9:0
                               run [3]            100.0%
--------------------------------------------------------
                               profile-thunk14 [2]100.0%
[3] 5666(100.0%)    0(0.0%)  run ...pkgs/profile-pkgs/profile-lib/main.rkt:31:2
                               for-loop [4]       100.0%
--------------------------------------------------------
                               run [3]            100.0%
[4] 5666(100.0%) 1630(28.8%) for-loop /tmp/profile.rkt:12:2
                               f [5]               71.2%
--------------------------------------------------------
                               for-loop [4]       100.0%
[5] 4036(71.2%)  1786(31.5%) f /tmp/profile.rkt:5:2
                               for-loop [6]        55.8%
--------------------------------------------------------
                               f [5]              100.0%
[6] 2250(39.7%)  2250(39.7%) for-loop /tmp/profile.rkt:6:4
--------------------------------------------------------

Note that it does show line numbers, so that even though there are two functions named f, it is possible to see which one -- and in fact which part of each one.


Also I strongly recommend the Optimization Coach package. Although this will give you different insights than the traditional profiler, it also gives you specific suggestions how to change the code to likely be faster. As a by-product it teaches you how to write it that way in the first place.

Sign up to request clarification or add additional context in comments.

4 Comments

Equivalent Racket documentation has ZERO(!) examples. Really surprising. And profiling just expressions is not enough (read: no line number). Ok for toying, not ok for a bit larger code imo. Especially if my inner functions have the same names. As for thoroughness.. I see lots of blog posts for "python profiling". Nothing for racket again. Thanks for "opt. coach" will definitely take a look.
Actually it does show line numbers. Please see my updated answer with slightly bigger example, and the output.
p.s. Although I think the Racket documentation overall is very good, I agree that it could use more examples.
@bravmech If this answered the question that you asked, would you please mark it as accepted? Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.