-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.sld
86 lines (66 loc) · 2.41 KB
/
demo.sld
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(define-library (demo)
(import (gambit))
(import (..)) ;; relative import of statprof (preserves the version)
;; in a typical program you would use this import clause to
;; get version 0.0.1 of the statprof library:
;;
;; (import (github.com/gambit/statprof @0.0.1))
(begin
(define (joes-challenge n m)
(define (create-threads)
(declare (fixnum) (not safe))
(let ((threads (make-vector n)))
(for-each
(lambda (i)
(vector-set!
threads
i
(make-thread
(lambda ()
(let ((output (vector-ref threads (modulo (+ i 1) n))))
(let loop ((j m))
(if (> j 0)
(let ((message (thread-receive)))
(thread-send output message)
(loop (- j 1))))))))))
(iota n))
threads))
(let* ((point1
(cpu-time))
(threads
(create-threads))
(point2
(cpu-time))
(_
(vector-for-each thread-start! threads))
(point3
(cpu-time)))
(thread-send (vector-ref threads 0) 'go)
(thread-join! (vector-ref threads (- m 1)))
(let ((point4
(cpu-time)))
(println "number of threads: "
n)
(println "number of messages received/sent by each thread: "
m)
(println "thread creation time (per thread in nsecs): "
(* 1000000000. (/ (- point2 point1) n)))
(println "thread start time (per thread in nsecs): "
(* 1000000000. (/ (- point3 point2) n)))
(println "message receive/send time (per message in nsecs): "
(* 1000000000. (/ (- point4 point3) (* n m)))))))
(define (ring n)
(if (and (integer? n)
(exact? n)
(>= n 2)
(<= n 1000000))
(let ((m (quotient 10000000 n)))
(joes-challenge n m))
(error "invalid number of threads")))
(statprof-start! '(profile flamegraph))
(ring 10000)
(statprof-stop!)
(let ((output-dir "statprof-demo-profile"))
(statprof-write! output-dir)
(println "To view the profile open " output-dir "/index.html"))
(println "Demo source code: " (this-source-file))))