[1b29ac]: inst / benchmark_index.m  Maximize  Restore  History

Download this file

175 lines (143 with data), 3.5 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
% Copyright (C) 2009 VZLU Prague
%
% This file is part of OctaveForge.
%
% OctaveForge is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this software; see the file COPYING. If not, see
% <http://www.gnu.org/licenses/>.
%
% function benchmark_index (n, rep)
% description:
% Test speed of array indexing.
%
% arguments:
% n = array size
% rep = number of repeats
%
% results:
% time_slice1 = time for a(i:j)
% time_slice1s = time for a(i:k:j)
% time_slice1v = time for a(idx)
% time_slice2c = time for a(:,i:j)
% time_slice2r = time for a(i:j,:)
% time_slice2cv = time for a(:,idx)
% time_slice2rv = time for a(idx,:)
% time_slicenc = time for a(:,:,i:j,k)
% time_slicend = time for a(:,:,k,i:j)
% time_slicens = time for a(i,j,k,:)
% time_spreadr = time for a(ones(1, k), :), a row vector
% time_spreadc = time for a(:, ones(1, k)), a column vector
%
function results = benchmark_index (n, rep)
benchutil_default_arg ('n', 4e6);
benchutil_default_arg ('rep', 100);
benchutil_initialize (mfilename)
a = rand (1, n);
i = 10;
j = n - 10;
tic;
for irep = 1:rep
b = a(i:j);
end
time_slice1 = toc;
benchutil_set_result ('time_slice1')
k = 2;
tic;
for irep = 1:rep
b = a(i:k:j);
end
time_slice1s = toc;
benchutil_set_result ('time_slice1s')
idx = cumsum (rand (1, n));
idx = ceil (idx / idx(end) * n);
tic;
for irep = 1:rep
b = a(idx);
end
time_slice1v = toc;
benchutil_set_result ('time_slice1v')
m = floor (sqrt(n));
a = rand (m);
i = 5;
j = m - 5;
tic;
for irep = 1:rep
b = a(:,i:j);
end
time_slice2c = toc;
benchutil_set_result ('time_slice2c')
tic;
for irep = 1:rep
b = a(i:j,:);
end
time_slice2r = toc;
benchutil_set_result ('time_slice2r')
idx = cumsum (rand (1, m));
idx = ceil (idx / idx(end) * m);
tic;
for irep = 1:rep
b = a(:,idx);
end
time_slice2cv = toc;
benchutil_set_result ('time_slice2cv')
tic;
for irep = 1:rep
b = a(idx,:);
end
time_slice2rv = toc;
benchutil_set_result ('time_slice2rv')
m = floor (sqrt (n / 6));
a = rand (2, m, m, 3);
i = 5;
j = m - 5;
k = 2;
tic;
for irep = 1:rep
b = a(:,:,i:j,k);
end
time_slicenc = toc;
benchutil_set_result ('time_slicenc')
m = floor (sqrt (n / 6));
a = rand (2, m, 3, m);
i = 5;
j = m - 5;
k = 2;
tic;
for irep = 1:rep
b = a(:,:,k,i:j);
end
time_slicend = toc;
benchutil_set_result ('time_slicend')
m = floor (n / 12);
a = rand (2, 2, 3, m);
tic;
for irep = 1:rep
b = a(2,1,3,:);
end
time_slicens = toc;
benchutil_set_result ('time_slicens')
m = floor (sqrt (n));
a = rand (1, m);
tic;
for irep = 1:rep
b = a(ones(1, m), :);
end
time_spreadr = toc;
benchutil_set_result ('time_spreadr')
a = rand (m, 1);
tic;
for irep = 1:rep
b = a(:, ones(1, m));
end
time_spreadc = toc;
benchutil_set_result ('time_spreadc')