author | Tony Garnock-Jones <tonygarnockjones@gmail.com> |
Tue, 17 Jul 2018 13:26:57 +0100 | |
changeset 390 | bfd7d4e7f498 |
child 391 | 1d4b8c5e18a4 |
permissions | -rw-r--r-- |
390
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
1 |
#lang racket |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
2 |
;; Crude measurement of techniques for patching functions during runtime. |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
3 |
;; |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
4 |
;; 2018-07-17 13:25:34 With Racket v6.90, unboxing is superior to |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
5 |
;; case-lambda; unboxing is only about 5% slower than not being able |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
6 |
;; to update the function at all. |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
7 |
|
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
8 |
(define-namespace-anchor ns-anchor) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
9 |
(define ns (namespace-anchor->namespace ns-anchor)) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
10 |
|
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
11 |
(define (main) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
12 |
(define N 50000000) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
13 |
|
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
14 |
(define (by-immutable) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
15 |
(define f (eval '(lambda (x) (+ x 1)) ns)) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
16 |
(for/fold [(x 0)] [(n (in-range N))] (f x))) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
17 |
|
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
18 |
(for [(i 5)] (time (by-immutable))) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
19 |
(newline) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
20 |
|
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
21 |
(define (by-unboxing) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
22 |
(define f (eval '(box (lambda (x) (+ x 1))) ns)) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
23 |
(for/fold [(x 0)] [(n (in-range N))] ((unbox f) x))) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
24 |
|
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
25 |
(for [(i 5)] (time (by-unboxing))) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
26 |
(newline) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
27 |
|
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
28 |
(define (by-embedding) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
29 |
(define f (eval '(let ((inner-f (lambda (x) (+ x 1)))) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
30 |
(case-lambda |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
31 |
[() (lambda (new-f) (set! inner-f new-f))] |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
32 |
[(x) (inner-f x)])) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
33 |
ns)) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
34 |
(for/fold [(x 0)] [(n (in-range N))] (f x))) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
35 |
|
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
36 |
(for [(i 5)] (time (by-embedding))) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
37 |
(newline)) |
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
38 |
|
bfd7d4e7f498
Crude measurement of techniques for patching functions.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff
changeset
|
39 |
(module+ main (main)) |