-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf-cyclone.scm
136 lines (116 loc) · 3.77 KB
/
perf-cyclone.scm
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(import (scheme base)
(scheme write)
(scheme file)
(cyclone benchmark)
(cyclone foreign)
(cyclone printf)
(srfi 1)
(srfi 27)) ;; random numbers
;; (c-define printf double "printf" string string string double)
(define (print-perf test-name bench-result)
(printf "%s,%s,%.9f\n" "cyclone" test-name bench-result))
(define (fib n)
(if (< n 2)
n
(+ (fib (- n 2)) (fib (- n 2)))))
(print-perf "recursion_fibonacci" (elapsed (fib 20)))
(define (iterative-fib n)
(do ((n n (- n 1))
(current 0 next)
(next 1 (+ next current)))
((= n 0) current)))
(print-perf "iteration_fibonacci" (elapsed (iterative-fib 20)))
(define (quicksort lst)
(cond
((or (null? lst) ; empty list is sorted
(null? (cdr lst))) ; single-element list is sorted
lst)
(else
(let ((pivot (list-ref lst
(floor (/ (length lst) 2))))
(rest (cdr lst)))
(append
(quicksort ; Recursively sort the list of smaller values
(filter (lambda (x) (< x pivot)) rest)) ; Select the smaller values
(list pivot) ; Add the pivot in the middle
(quicksort ; Recursively sort the list of larger values
(filter (lambda (x) (>= x pivot)) rest))))))) ; Select the larger and equal values
(define-syntax while
(syntax-rules ()
((while condition body ...)
(let loop ()
(when condition
body ...
(loop))))))
(define (random-list n)
(let loop ((i 0)
(lst '()))
(if (= i n)
lst
(loop (+ i 1) (cons (random-real) lst)))))
(print-perf "functional_recursion_quicksort" (elapsed (quicksort (random-list 5000))))
(define (quicksort! vec lo hi)
(define i lo)
(define j hi)
(while (< i hi)
(let ((pivot (vector-ref vec (floor (/ (+ lo hi) 2)))))
(while (<= i j)
(while (< (vector-ref vec i) pivot)
(set! i (+ i 1)))
(while (> (vector-ref vec j) pivot)
(set! j (- j 1)))
(when (<= i j)
(let ((e (vector-ref vec i)))
(vector-set! vec i (vector-ref vec j))
(vector-set! vec j e)
(set! i (+ i 1))
(set! j (- j 1)))))
(when (< lo j)
(quicksort! vec lo j))
(set! lo i)
(set! j hi)))
vec)
(define (random-vector n)
(do ((v (make-vector n))
(i 0 (+ i 1)))
((= i n) v)
(vector-set! v i (random-real))))
(print-perf "imperative_recursion_quicksort" (elapsed (quicksort! (random-vector 5000) 0 4999)))
(define-syntax for
(syntax-rules (in as)
((for element in list
body ...)
(for-each (lambda (element)
body ...)
list))
((for list as element
body ...)
(for element in list
body ...))))
(define (pisum)
(define sum 0.0)
(for j in (iota 500 1)
(set! sum 0.0)
(for k in (iota 10000 1)
(set! sum (+ sum (/ 1.0 (* k k))))))
sum)
(print-perf "iteration_pi_sum" (elapsed (pisum)))
(define (printfd n)
(with-output-to-file "/dev/null"
(lambda ()
(let loop ((i 0))
(when (< i n)
(display i)
(display " ")
(display (+ i 1))
(newline)
(loop (+ i 1)))))))
(print-perf "print_to_file" (elapsed (printfd 100000)))
(define (parse-int t)
(for i in (iota t 1)
(let* ((n (random-integer (- (expt 2 32) 1)))
(s (number->string n 16))
(m (string->number s 16)))
(unless (= n m) (error "Incorrect parsing of integers!"))
n)))
(print-perf "parse_integers" (elapsed (parse-int 1000)))