Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thorough guide for profiling racket code

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.

like image 272
bravmi Avatar asked Jun 02 '14 06:06

bravmi


1 Answers

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.

like image 162
Greg Hendershott Avatar answered Jan 02 '23 19:01

Greg Hendershott