-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathperf.jl
More file actions
149 lines (121 loc) · 3.02 KB
/
perf.jl
File metadata and controls
149 lines (121 loc) · 3.02 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
# This file was formerly a part of Julia. License is MIT: https://julialang.org/license
using LinearAlgebra: tr
using Printf: @printf
using Random: seed!, rand, randn
using Statistics: std, mean
using Test: @test
const NITER = 5
seed!(1776)
macro timeit(ex, name)
quote
t = zeros(NITER)
for i = 0:NITER
e = 1000 * (@elapsed $(esc(ex)))
if i > 0
t[i] = e # i == 0 is JIT warmup
end
end
@printf "julia,%s,%f\n" $(esc(name)) minimum(t)
GC.gc()
end
end
## recursive fib ##
fib(n) = n < 2 ? n : fib(n - 1) + fib(n - 2)
@test fib(20) == 6765
@timeit fib(20) "recursion_fibonacci"
## parse integer ##
function parseintperf(t)
local n
for i = 1:t
n = rand(UInt32)
s = string(n, base=16)
m = parse(UInt32, s, base=16)
@assert m == n
end
return n
end
@timeit parseintperf(1000) "parse_integers"
## mandelbrot set: complex arithmetic and comprehensions ##
function mandel(z)
c = z
maxiter = 80
for n = 1:maxiter
if real(z)*real(z) + imag(z)*imag(z) > 4
return n - 1
end
z = z^2 + c
end
return maxiter
end
mandelperf() = [mandel(complex(r, i)) for i = -1.0:0.1:1.0, r = -2.0:0.1:0.5]
@test sum(mandelperf()) == 14791
@timeit mandelperf() "userfunc_mandelbrot"
## numeric vector sort ##
function qsort!(a, lo, hi)
i, j = lo, hi
while i < hi
pivot = a[(lo+hi)>>>1]
while i <= j
while a[i] < pivot; i += 1; end
while a[j] > pivot; j -= 1; end
if i <= j
a[i], a[j] = a[j], a[i]
i, j = i + 1, j - 1
end
end
if lo < j; qsort!(a, lo, j); end
lo, j = i, hi
end
return a
end
sortperf(n) = qsort!(rand(n), 1, n)
@test issorted(sortperf(5000))
@timeit sortperf(5000) "recursion_quicksort"
## slow pi series ##
const _pisum_vol = Ref(0.0)
function pisum()
for j = 1:500
s = 0.0
for k = 1:10000
s += 1.0 / (k * k)
end
_pisum_vol[] = s
end
return _pisum_vol[]
end
@test abs(pisum() - 1.644834071848065) < 1e-12
@timeit pisum() "iteration_pi_sum"
## random matrix statistics ##
function randmatstat(t)
n = 5
v = zeros(t)
w = zeros(t)
for i = 1:t
a = randn(n, n)
b = randn(n, n)
c = randn(n, n)
d = randn(n, n)
P = [a b c d]
Q = [a b; c d]
v[i] = tr((P'P)^4)
w[i] = tr((Q'Q)^4)
end
return (std(v) / mean(v), std(w) / mean(w))
end
(s1, s2) = randmatstat(1000)
@test 0.5 < s1 < 1.0 && 0.5 < s2 < 1.0
@timeit randmatstat(1000) "matrix_statistics"
## largish random number gen & matmul ##
@timeit rand(1000, 1000) * rand(1000, 1000) "matrix_multiply"
## printfd ##
if Sys.isunix()
function printfd(n)
open("/dev/null", "w") do io
for i = 1:n
@printf(io, "%d %d\n", i, i + 1)
end
end
end
printfd(1)
@timeit printfd(100000) "print_to_file"
end