forked from JuliaLang/Microbenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf.m
234 lines (187 loc) · 4.31 KB
/
perf.m
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Main function. All the tests are run here. %%
%% The functions declarations can be found at the end. %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function perf()
warning off;
f = fib(20);
assert(f == 6765)
timeit('recursion_fibonacci', @fib, 20)
timeit('parse_integers', @parseintperf, 1000)
%% array constructors %%
%o = ones(200,200);
%assert(all(o) == 1)
%timeit('ones', @ones, 200, 200)
%assert(all(matmul(o) == 200))
%timeit('AtA', @matmul, o)
mandel(complex(-.53,.68));
assert(sum(sum(mandelperf(true))) == 14791)
timeit('userfunc_mandelbrot', @mandelperf, true)
assert(issorted(sortperf(5000)))
timeit('recursion_quicksort', @sortperf, 5000)
s = pisum(true);
assert(abs(s-1.644834071848065) < 1e-12);
timeit('iteration_pi_sum',@pisum, true)
%s = pisumvec(true);
%assert(abs(s-1.644834071848065) < 1e-12);
%timeit('pi_sum_vec',@pisumvec, true)
[s1, s2] = randmatstat(1000);
assert(round(10*s1) > 5 && round(10*s1) < 10);
timeit('matrix_statistics', @randmatstat, 1000)
timeit('matrix_multiply', @randmatmul, 1000);
printfd(1)
timeit('print_to_file', @printfd, 100000)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Functions declarations %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function assert(bool)
if ~bool
error('Assertion failed')
end
end
function timeit(name, func, varargin)
lang = 'matlab';
if exist('OCTAVE_VERSION') ~= 0
lang = 'octave';
end
nexpt = 5;
times = zeros(nexpt, 1);
for i=1:nexpt
tic(); func(varargin{:}); times(i) = toc();
end
times = sort(times);
fprintf ('%s,%s,%.8f\n', lang, name, times(1)*1000);
end
%% recursive fib %%
function f = fib(n)
if n < 2
f = n;
return
else
f = fib(n-1) + fib(n-2);
end
end
%% parse int %%
function n = parseintperf(t)
for i = 1:t
n = fix(rand*(2^32));
s = sprintf('%08x',n);
m = sscanf(s,'%x');
assert(m == n);
end
end
%% matmul and transpose %%
%function oo = matmul(o)
% oo = o * o.';
%end
%% mandelbrot set: complex arithmetic and comprehensions %%
function r = abs2(z)
r = real(z)*real(z) + imag(z)*imag(z);
end
function n = mandel(z)
n = 0;
c = z;
for n=0:79
if abs2(z)>4
return
end
z = z^2+c;
end
n = 80;
end
function M = mandelperf(ignore)
x=-2.0:.1:0.5;
y=-1:.1:1;
M=zeros(length(y),length(x));
for r=1:size(M,1)
for c=1:size(M,2)
M(r,c) = mandel(x(c)+y(r)*i);
end
end
end
%% numeric vector quicksort %%
function b = qsort(a)
b = qsort_kernel(a, 1, length(a));
end
function a = qsort_kernel(a, lo, hi)
i = lo;
j = hi;
while i < hi
pivot = a(floor((lo+hi)/2));
while i <= j
while a(i) < pivot, i = i + 1; end
while a(j) > pivot, j = j - 1; end
if i <= j
t = a(i);
a(i) = a(j);
a(j) = t;
i = i + 1;
j = j - 1;
end
end
if lo < j; a=qsort_kernel(a, lo, j); end
lo = i;
j = hi;
end
end
function v = sortperf(n)
v = rand(n,1);
v = qsort(v);
end
%% slow pi series %%
function sum = pisum(ignore)
sum = 0.0;
for j=1:500
sum = 0.0;
for k=1:10000
sum = sum + 1.0/(k*k);
end
end
end
%% slow pi series, vectorized %%
function s = pisumvec(ignore)
a = [1:10000];
for j=1:500
s = sum( 1./(a.^2));
end
end
%% random matrix statistics %%
function [s1, s2] = randmatstat(t)
n=5;
v = zeros(t,1);
w = zeros(t,1);
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) = trace((P.'*P)^4);
w(i) = trace((Q.'*Q)^4);
end
s1 = std(v)/mean(v);
s2 = std(w)/mean(w);
end
function t = mytranspose(x)
[m, n] = size(x);
t = zeros(n, m);
for i=1:n
for j=1:m
t(i,j) = x(j,i);
end
end
end
%% largish random number gen & matmul %%
function X = randmatmul(n)
X = rand(n,n)*rand(n,n);
end
%% printf %%
function printfd(n)
f = fopen('/dev/null','w');
for i = 1:n
fprintf(f, '%d %d\n', i, i + 1);
end
fclose(f);
end