[7867b6]: inst / benchmark_intmath.m Maximize Restore History

Download this file

benchmark_intmath.m    183 lines (140 with data), 6.1 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
174
175
176
177
178
179
180
% Copyright (C) 2008 Jaroslav Hajek <highegg@gmail.com>
%
% 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_intmath (n, ratio)
% description:
% Test speed of integer math & conversions.
%
% arguments:
% n = array size
% ratio = ratio of intmath for generating integers
%
% results:
% time_uint8_conv = time to convert real vector to uint8
% time_uint8_add = time to add two uint8 vectors
% time_uint8_sub = time to subtract two uint8 vectors
% time_uint8_mul = time to multiply two uint8 vectors
% time_uint8_div = time to divide two uint8 vectors
% time_int8_conv = time to convert real vector to int8
% time_int8_add = time to add two int8 vectors
% time_int8_sub = time to subtract two int8 vectors
% time_int8_mul = time to multiply two int8 vectors
% time_int8_div = time to divide two int8 vectors
% time_uint16_conv = time to convert real vector to uint16
% time_uint16_add = time to add two uint16 vectors
% time_uint16_sub = time to subtract two uint16 vectors
% time_uint16_mul = time to multiply two uint16 vectors
% time_uint16_div = time to divide two uint16 vectors
% time_int16_conv = time to convert real vector to int16
% time_int16_add = time to add two int16 vectors
% time_int16_sub = time to subtract two int16 vectors
% time_int16_mul = time to multiply two int16 vectors
% time_int16_div = time to divide two int16 vectors
% time_uint32_conv = time to convert real vector to uint32
% time_uint32_add = time to add two uint32 vectors
% time_uint32_sub = time to subtract two uint32 vectors
% time_uint32_mul = time to multiply two uint32 vectors
% time_uint32_div = time to divide two uint32 vectors
% time_int32_conv = time to convert real vector to int32
% time_int32_add = time to add two int32 vectors
% time_int32_sub = time to subtract two int32 vectors
% time_int32_mul = time to multiply two int32 vectors
% time_int32_div = time to divide two int32 vectors
%
function results = benchmark_intmath (n, ratio)
benchutil_default_arg ('n', 1e7);
benchutil_default_arg ('ratio', 0.6);
benchutil_initialize (mfilename)
x = ratio * double (intmax ('uint8')) * rand(n, 1);
y = ratio * double (intmax ('uint8')) * rand(n, 1);
x = uint8 (x);
tic; y = uint8 (y); time_uint8_conv = toc;
benchutil_set_result ('time_uint8_conv')
tic; xy = x + y; time_uint8_add = toc;
benchutil_set_result ('time_uint8_add')
tic; xy = x - y; time_uint8_sub = toc;
benchutil_set_result ('time_uint8_sub')
tic; xy = x .* y; time_uint8_mul = toc;
benchutil_set_result ('time_uint8_mul')
tic; xy = x ./ y; time_uint8_div = toc;
benchutil_set_result ('time_uint8_div')
x = ratio * double (intmax ('int8')) * (2 * rand(n, 1) - 1);
y = ratio * double (intmax ('int8')) * (2 * rand(n, 1) - 1);
x = int8 (x);
tic; y = int8 (y); time_int8_conv = toc;
benchutil_set_result ('time_int8_conv')
tic; xy = x + y; time_int8_add = toc;
benchutil_set_result ('time_int8_add')
tic; xy = x - y; time_int8_sub = toc;
benchutil_set_result ('time_int8_sub')
tic; xy = x .* y; time_int8_mul = toc;
benchutil_set_result ('time_int8_mul')
tic; xy = x ./ y; time_int8_div = toc;
benchutil_set_result ('time_int8_div')
x = ratio * double (intmax ('uint16')) * rand(n, 1);
y = ratio * double (intmax ('uint16')) * rand(n, 1);
x = uint16 (x);
tic; y = uint16 (y); time_uint16_conv = toc;
benchutil_set_result ('time_uint16_conv')
tic; xy = x + y; time_uint16_add = toc;
benchutil_set_result ('time_uint16_add')
tic; xy = x - y; time_uint16_sub = toc;
benchutil_set_result ('time_uint16_sub')
tic; xy = x .* y; time_uint16_mul = toc;
benchutil_set_result ('time_uint16_mul')
tic; xy = x ./ y; time_uint16_div = toc;
benchutil_set_result ('time_uint16_div')
x = ratio * double (intmax ('int16')) * (2 * rand(n, 1) - 1);
y = ratio * double (intmax ('int16')) * (2 * rand(n, 1) - 1);
x = int16 (x);
tic; y = int16 (y); time_int16_conv = toc;
benchutil_set_result ('time_int16_conv')
tic; xy = x + y; time_int16_add = toc;
benchutil_set_result ('time_int16_add')
tic; xy = x - y; time_int16_sub = toc;
benchutil_set_result ('time_int16_sub')
tic; xy = x .* y; time_int16_mul = toc;
benchutil_set_result ('time_int16_mul')
tic; xy = x ./ y; time_int16_div = toc;
benchutil_set_result ('time_int16_div')
x = ratio * double (intmax ('uint32')) * rand(n, 1);
y = ratio * double (intmax ('uint32')) * rand(n, 1);
x = uint32 (x);
tic; y = uint32 (y); time_uint32_conv = toc;
benchutil_set_result ('time_uint32_conv')
tic; xy = x + y; time_uint32_add = toc;
benchutil_set_result ('time_uint32_add')
tic; xy = x - y; time_uint32_sub = toc;
benchutil_set_result ('time_uint32_sub')
tic; xy = x .* y; time_uint32_mul = toc;
benchutil_set_result ('time_uint32_mul')
tic; xy = x ./ y; time_uint32_div = toc;
benchutil_set_result ('time_uint32_div')
x = ratio * double (intmax ('int32')) * (2 * rand(n, 1) - 1);
y = ratio * double (intmax ('int32')) * (2 * rand(n, 1) - 1);
x = int32 (x);
tic; y = int32 (y); time_int32_conv = toc;
benchutil_set_result ('time_int32_conv')
tic; xy = x + y; time_int32_add = toc;
benchutil_set_result ('time_int32_add')
tic; xy = x - y; time_int32_sub = toc;
benchutil_set_result ('time_int32_sub')
tic; xy = x .* y; time_int32_mul = toc;
benchutil_set_result ('time_int32_mul')
tic; xy = x ./ y; time_int32_div = toc;
benchutil_set_result ('time_int32_div')