forked from JuliaLang/Microbenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf.jl
173 lines (138 loc) · 3.81 KB
/
perf.jl
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# This file was formerly a part of Julia. License is MIT: https://julialang.org/license
using Compat
import Compat.LinearAlgebra
import Compat.Test
import Compat.Printf
import Compat.Statistics
import Compat.Sys
include("./perfutil.jl")
## recursive fib ##
fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
@compat Test.@test fib(20) == 6765
@timeit fib(20) "recursion_fibonacci" "Recursive fibonacci"
## parse integer ##
function parseintperf(t)
local n, m
for i=1:t
n = rand(UInt32)
@static if VERSION >= v"0.7.0-DEV.4446"
s = string(n, base = 16)
m = UInt32(parse(Int64, s, base = 16))
else
s = hex(n)
m = UInt32(parse(Int64, s, 16))
end
@assert m == n
end
return n
end
@timeit parseintperf(1000) "parse_integers" "Integer parsing"
## array constructors ##
@compat Test.@test all(fill(1.,200,200) .== 1)
## matmul and transpose ##
A = fill(1.,200,200)
@compat Test.@test all(A*A' .== 200)
# @timeit A*A' "AtA" "description"
## mandelbrot set: complex arithmetic and comprehensions ##
function myabs2(z)
return real(z)*real(z) + imag(z)*imag(z)
end
function mandel(z)
c = z
maxiter = 80
for n = 1:maxiter
if myabs2(z) > 4
return n-1
end
z = z^2 + c
end
return maxiter
end
mandelperf() = [ mandel(complex(r,i)) for i=-1.:.1:1., r=-2.0:.1:0.5 ]
@compat Test.@test sum(mandelperf()) == 14791
@timeit mandelperf() "userfunc_mandelbrot" "Calculation of mandelbrot set"
## 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)
@compat Test.@test issorted(sortperf(5000))
@timeit sortperf(5000) "recursion_quicksort" "Sorting of random numbers using quicksort"
## slow pi series ##
function pisum()
sum = 0.0
for j = 1:500
sum = 0.0
for k = 1:10000
sum += 1.0/(k*k)
end
end
sum
end
@compat Test.@test abs(pisum()-1.644834071848065) < 1e-12
@timeit pisum() "iteration_pi_sum" "Summation of a power series"
## slow pi series, vectorized ##
function pisumvec()
s = 0.0
a = [1:10000]
for j = 1:500
s = sum(1 ./ (a.^2))
end
s
end
#@test abs(pisumvec()-1.644834071848065) < 1e-12
#@timeit pisumvec() "pi_sum_vec"
## 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]
@static if VERSION >= v"0.7.0"
v[i] = LinearAlgebra.tr((P'*P)^4)
w[i] = LinearAlgebra.tr((Q'*Q)^4)
else
v[i] = trace((P'*P)^4)
w[i] = trace((Q'*Q)^4)
end
end
@compat return (Statistics.std(v)/Statistics.mean(v), Statistics.std(w)/Statistics.mean(w))
end
(s1, s2) = randmatstat(1000)
@compat Test.@test 0.5 < s1 < 1.0 && 0.5 < s2 < 1.0
@timeit randmatstat(1000) "matrix_statistics" "Statistics on a random matrix"
## largish random number gen & matmul ##
@timeit rand(1000,1000)*rand(1000,1000) "matrix_multiply" "Multiplication of random matrices"
## printfd ##
@compat if Sys.isunix()
function printfd(n)
open("/dev/null", "w") do io
for i = 1:n
@compat Printf.@printf(io, "%d %d\n", i, i + 1)
end
end
end
printfd(1)
@timeit printfd(100000) "print_to_file" "Printing to a file descriptor"
end
#maxrss("micro")