]> Creatis software - CreaPhase.git/blob - octave_packages/benchmark-1.1.1/benchmark_index.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / benchmark-1.1.1 / benchmark_index.m
1 % Copyright (C) 2009  VZLU Prague
2
3 % This file is part of OctaveForge.
4
5 % OctaveForge is free software; you can redistribute it and/or modify
6 % it under the terms of the GNU General Public License as published by
7 % the Free Software Foundation; either version 2 of the License, or
8 % (at your option) any later version.
9
10 % This program is distributed in the hope that it will be useful,
11 % but WITHOUT ANY WARRANTY; without even the implied warranty of
12 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 % GNU General Public License for more details.
14
15 % You should have received a copy of the GNU General Public License
16 % along with this software; see the file COPYING.  If not, see
17 % <http://www.gnu.org/licenses/>.
18
19
20 % function benchmark_index (n, rep)
21 % description:
22 % Test speed of array indexing.
23 %
24 % arguments:
25 % n = array size
26 % rep = number of repeats
27 %
28 % results:
29 % time_slice1 = time for a(i:j)
30 % time_slice1s = time for a(i:k:j)
31 % time_slice1v = time for a(idx)
32 % time_slice2c = time for a(:,i:j)
33 % time_slice2r = time for a(i:j,:)
34 % time_slice2cv = time for a(:,idx)
35 % time_slice2rv = time for a(idx,:)
36 % time_slicenc = time for a(:,:,i:j,k)
37 % time_slicend = time for a(:,:,k,i:j)
38 % time_slicens = time for a(i,j,k,:)
39 % time_spreadr = time for a(ones(1, k), :), a row vector
40 % time_spreadc = time for a(:, ones(1, k)), a column vector
41 %
42
43 function results = benchmark_index (n, rep)
44
45   benchutil_default_arg ('n', 4e6);
46   benchutil_default_arg ('rep', 100);
47
48   benchutil_initialize (mfilename)
49
50   a = rand (1, n);
51
52   i = 10; 
53   j = n - 10;
54
55   tic; 
56   for irep = 1:rep
57     b = a(i:j);
58   end
59   time_slice1 = toc;
60   benchutil_set_result ('time_slice1')
61
62   k = 2;
63
64   tic; 
65   for irep = 1:rep
66     b = a(i:k:j);
67   end
68   time_slice1s = toc;
69   benchutil_set_result ('time_slice1s')
70
71   idx = cumsum (rand (1, n));
72   idx = ceil (idx / idx(end) * n);
73
74   tic; 
75   for irep = 1:rep
76     b = a(idx);
77   end
78   time_slice1v = toc;
79   benchutil_set_result ('time_slice1v')
80
81   m = floor (sqrt(n));
82   a = rand (m);
83
84   i = 5; 
85   j = m - 5;
86
87   tic; 
88   for irep = 1:rep
89     b = a(:,i:j);
90   end
91   time_slice2c = toc;
92   benchutil_set_result ('time_slice2c')
93
94   tic; 
95   for irep = 1:rep
96     b = a(i:j,:);
97   end
98   time_slice2r = toc;
99   benchutil_set_result ('time_slice2r')
100
101   idx = cumsum (rand (1, m));
102   idx = ceil (idx / idx(end) * m);
103
104   tic; 
105   for irep = 1:rep
106     b = a(:,idx);
107   end
108   time_slice2cv = toc;
109   benchutil_set_result ('time_slice2cv')
110
111   tic; 
112   for irep = 1:rep
113     b = a(idx,:);
114   end
115   time_slice2rv = toc;
116   benchutil_set_result ('time_slice2rv')
117
118   m = floor (sqrt (n / 6));
119   a = rand (2, m, m, 3);
120
121   i = 5;
122   j = m - 5;
123   k = 2;
124
125   tic; 
126   for irep = 1:rep
127     b = a(:,:,i:j,k);
128   end
129   time_slicenc = toc;
130   benchutil_set_result ('time_slicenc')
131
132   m = floor (sqrt (n / 6));
133   a = rand (2, m, 3, m);
134
135   i = 5;
136   j = m - 5;
137   k = 2;
138
139   tic; 
140   for irep = 1:rep
141     b = a(:,:,k,i:j);
142   end
143   time_slicend = toc;
144   benchutil_set_result ('time_slicend')
145
146   m = floor (n / 12);
147   a = rand (2, 2, 3, m);
148
149   tic; 
150   for irep = 1:rep
151     b = a(2,1,3,:);
152   end
153   time_slicens = toc;
154   benchutil_set_result ('time_slicens')
155
156   m = floor (sqrt (n));
157   a = rand (1, m);
158
159   tic; 
160   for irep = 1:rep
161     b = a(ones(1, m), :);
162   end
163   time_spreadr = toc;
164   benchutil_set_result ('time_spreadr')
165
166   a = rand (m, 1);
167
168   tic; 
169   for irep = 1:rep
170     b = a(:, ones(1, m));
171   end
172   time_spreadc = toc;
173   benchutil_set_result ('time_spreadc')
174