Skip to content

Commit 177a520

Browse files
committed
Merge pull request #8847 from pabloferz/pf/perf
Microbenchmarks fixes (C, R, Octave)
2 parents f6d419a + 0121d48 commit 177a520

File tree

3 files changed

+58
-44
lines changed

3 files changed

+58
-44
lines changed

test/perf/micro/perf.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ timeit("fib", fib, 20)
3232
parseintperf = function(t) {
3333
for (i in 1:t) {
3434
# R doesn't support uint32 values
35-
n = floor(runif(1, min=0, max=2^31-1))
35+
n = floor(runif(1, min=0, max=2^32-1))
3636
s = sprintf("0x%x", n)
3737
m = as.numeric(s)
3838
assert(m == n)

test/perf/micro/perf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,10 @@ int main() {
238238
assert(fib(20) == 6765);
239239
int f = 0;
240240
tmin = INFINITY;
241+
volatile int fibarg = 20; // prevent constant propagation
241242
for (int i=0; i<NITER; ++i) {
242243
t = clock_now();
243-
f += fib(20);
244+
f += fib(fibarg);
244245
t = clock_now()-t;
245246
if (t < tmin) tmin = t;
246247
}

test/perf/micro/perf.m

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
1+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2+
%% Main function. All the tests are run here. %%
3+
%% The functions declarations can be found at the end. %%
4+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5+
16
function perf()
27

3-
warning off;
4-
if exist('OCTAVE_VERSION') == 0
5-
maxNumCompThreads(1);
6-
end
8+
warning off;
9+
if exist('OCTAVE_VERSION') == 0
10+
maxNumCompThreads(1);
11+
end
12+
13+
f = fib(20);
14+
assert(f == 6765)
15+
timeit('fib', @fib, 20)
16+
17+
timeit('parse_int', @parseintperf, 1000)
18+
19+
%% array constructors %%
20+
21+
%o = ones(200,200);
22+
%assert(all(o) == 1)
23+
%timeit('ones', @ones, 200, 200)
24+
25+
%assert(all(matmul(o) == 200))
26+
%timeit('AtA', @matmul, o)
27+
28+
mandel(complex(-.53,.68));
29+
assert(sum(sum(mandelperf(true))) == 14791)
30+
timeit('mandel', @mandelperf, true)
31+
32+
assert(issorted(sortperf(5000)))
33+
timeit('quicksort', @sortperf, 5000)
34+
35+
s = pisum(true);
36+
assert(abs(s-1.644834071848065) < 1e-12);
37+
timeit('pi_sum',@pisum, true)
38+
39+
%s = pisumvec(true);
40+
%assert(abs(s-1.644834071848065) < 1e-12);
41+
%timeit('pi_sum_vec',@pisumvec, true)
42+
43+
[s1, s2] = randmatstat(1000);
44+
assert(round(10*s1) > 5 && round(10*s1) < 10);
45+
timeit('rand_mat_stat', @randmatstat, 1000)
46+
47+
timeit('rand_mat_mul', @randmatmul, 1000);
48+
49+
printfd(1)
50+
timeit('printfd', @printfd, 100000)
51+
52+
end
53+
54+
55+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56+
%% Functions declarations %%
57+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758

859
function assert(bool)
960
if ~bool
@@ -39,10 +90,6 @@ function timeit(name, func, varargin)
3990
end
4091
end
4192

42-
f = fib(20);
43-
assert(f == 6765)
44-
timeit('fib', @fib, 20)
45-
4693
%% parse int %%
4794

4895
function n = parseintperf(t)
@@ -53,21 +100,12 @@ function timeit(name, func, varargin)
53100
assert(m == n);
54101
end
55102
end
56-
timeit('parse_int', @parseintperf, 1000)
57-
58-
%% array constructors %%
59-
60-
%o = ones(200,200);
61-
%assert(all(o) == 1)
62-
% timeit('ones', @ones, 200, 200)
63103

64104
%% matmul and transpose %%
65105

66106
%function oo = matmul(o)
67107
% oo = o * o.';
68108
%end
69-
%assert(all(matmul(o) == 200))
70-
% timeit('AtA', @matmul, o)
71109

72110
%% mandelbrot set: complex arithmetic and comprehensions %%
73111

@@ -83,8 +121,6 @@ function timeit(name, func, varargin)
83121
n = 80;
84122
end
85123

86-
mandel(complex(-.53,.68));
87-
88124
function M = mandelperf(ignore)
89125
M = zeros(length(-2.0:.1:0.5), length(-1:.1:1));
90126
count = 1;
@@ -95,8 +131,6 @@ function timeit(name, func, varargin)
95131
end
96132
end
97133
end
98-
assert(sum(sum(mandelperf(true))) == 14791)
99-
timeit('mandel', @mandelperf, true)
100134

101135
%% numeric vector quicksort %%
102136

@@ -130,8 +164,6 @@ function timeit(name, func, varargin)
130164
v = rand(n,1);
131165
v = qsort(v);
132166
end
133-
assert(issorted(sortperf(5000)))
134-
timeit('quicksort', @sortperf, 5000)
135167

136168
%% slow pi series %%
137169

@@ -145,10 +177,6 @@ function timeit(name, func, varargin)
145177
end
146178
end
147179

148-
s = pisum(true);
149-
assert(abs(s-1.644834071848065) < 1e-12);
150-
timeit('pi_sum',@pisum, true)
151-
152180
%% slow pi series, vectorized %%
153181

154182
function s = pisumvec(ignore)
@@ -158,10 +186,6 @@ function timeit(name, func, varargin)
158186
end
159187
end
160188

161-
%s = pisumvec(true);
162-
%assert(abs(s-1.644834071848065) < 1e-12);
163-
%timeit('pi_sum_vec',@pisumvec, true)
164-
165189
%% random matrix statistics %%
166190

167191
function [s1, s2] = randmatstat(t)
@@ -182,10 +206,6 @@ function timeit(name, func, varargin)
182206
s2 = std(w)/mean(w);
183207
end
184208

185-
[s1, s2] = randmatstat(1000);
186-
assert(round(10*s1) > 5 && round(10*s1) < 10);
187-
timeit('rand_mat_stat', @randmatstat, 1000)
188-
189209
function t = mytranspose(x)
190210
[m, n] = size(x);
191211
t = zeros(n, m);
@@ -202,8 +222,6 @@ function timeit(name, func, varargin)
202222
X = rand(n,n)*rand(n,n);
203223
end
204224

205-
timeit('rand_mat_mul', @randmatmul, 1000);
206-
207225
%% printf %%
208226

209227
function printfd(n)
@@ -213,8 +231,3 @@ function printfd(n)
213231
end
214232
fclose(f);
215233
end
216-
217-
printfd(1)
218-
timeit('printfd', @printfd, 100000)
219-
220-
end

0 commit comments

Comments
 (0)