arithmetics.cpp

Knödlseder Jürgen, 03/26/2013 03:04 AM

Download (7.7 KB)

 
1
/* __ Includes ___________________________________________________________ */
2
#include <iostream>
3
#include <cmath>
4
#include "arithmetics.hpp"
5

    
6

    
7
/***********************************************************************//**
8
 * @brief power of 2
9
 ***************************************************************************/
10
void power2(int num)
11
{
12
    // Start timer
13
    clock_t t_start = clock();
14

    
15
    // Loop using pow
16
    for (int i = 0; i < num; ++i) {
17
        double arg    = double(i);
18
        double result = std::pow(arg, 2.0);
19
    }
20

    
21
    // Stop timer
22
    double t_elapse_pow = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
23

    
24
    // Start timer
25
    t_start = clock();
26

    
27
    // Loop using multiplication
28
    for (int i = 0; i < num; ++i) {
29
        double arg    = double(i);
30
        double result = arg*arg;
31
    }
32

    
33
    // Stop timer
34
    double t_elapse_mult = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
35

    
36
    // Dump result
37
    std::cout << "power2:";
38
    std::cout << " pow(x,2)=" << t_elapse_pow;
39
    std::cout << " x*x=" << t_elapse_mult;
40
    std::cout << " sec" << std::endl;
41

    
42
    // Return
43
    return;
44
}
45

    
46

    
47
/***********************************************************************//**
48
 * @brief power of 2p
49
 ***************************************************************************/
50
void power2p(int num)
51
{
52
    // Start timer
53
    clock_t t_start = clock();
54

    
55
    // Loop using pow
56
    for (int i = 0; i < num; ++i) {
57
        double arg    = double(i);
58
        double result = std::pow(arg, 2.01);
59
    }
60

    
61
    // Stop timer
62
    double t_elapse_pow = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
63

    
64
    // Start timer
65
    t_start = clock();
66

    
67
    // Loop using multiplication
68
    for (int i = 0; i < num; ++i) {
69
        double arg    = double(i);
70
        double result = arg*arg;
71
    }
72

    
73
    // Stop timer
74
    double t_elapse_mult = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
75

    
76
    // Dump result
77
    std::cout << "power2p:";
78
    std::cout << " pow(x,2.01)=" << t_elapse_pow;
79
    std::cout << " x*x=" << t_elapse_mult;
80
    std::cout << " sec" << std::endl;
81

    
82
    // Return
83
    return;
84
}
85

    
86

    
87
/***********************************************************************//**
88
 * @brief multidiv
89
 ***************************************************************************/
90
void multidiv(int num)
91
{
92
    // Set divider
93
    double div  = 3.0;
94
    double fact = 1.0 / div;
95

    
96
    // Start timer
97
    clock_t t_start = clock();
98

    
99
    // Loop using division
100
    for (int i = 0; i < num; ++i) {
101
        double arg    = double(i);
102
        double result = arg / div;
103
    }
104

    
105
    // Stop timer
106
    double t_elapse_div = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
107

    
108
    // Start timer
109
    t_start = clock();
110

    
111
    // Loop using multiplication
112
    for (int i = 0; i < num; ++i) {
113
        double arg    = double(i);
114
        double result = arg * fact;
115
    }
116

    
117
    // Stop timer
118
    double t_elapse_mult = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
119

    
120
    // Dump result
121
    std::cout << "multidiv:";
122
    std::cout << " div=" << t_elapse_div;
123
    std::cout << " mult=" << t_elapse_mult;
124
    std::cout << " sec" << std::endl;
125

    
126
    // Return
127
    return;
128
}
129

    
130

    
131
/***********************************************************************//**
132
 * @brief addsub
133
 ***************************************************************************/
134
void addsub(int num)
135
{
136
    // Start timer
137
    clock_t t_start = clock();
138

    
139
    // Loop using addition
140
    for (int i = 0; i < num; ++i) {
141
        double arg    = double(i);
142
        double result = arg + 1.5;
143
    }
144

    
145
    // Stop timer
146
    double t_elapse_add = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
147

    
148
    // Start timer
149
    t_start = clock();
150

    
151
    // Loop using subtraction
152
    for (int i = 0; i < num; ++i) {
153
        double arg    = double(i);
154
        double result = arg - 1.5;
155
    }
156

    
157
    // Stop timer
158
    double t_elapse_sub = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
159

    
160
    // Dump result
161
    std::cout << "addsub:";
162
    std::cout << " add=" << t_elapse_add;
163
    std::cout << " sub=" << t_elapse_sub;
164
    std::cout << " sec" << std::endl;
165

    
166
    // Return
167
    return;
168
}
169

    
170

    
171
/***********************************************************************//**
172
 * @brief sin, cos, tan, acos
173
 ***************************************************************************/
174
void sincos(int num)
175
{
176
    // Start timer
177
    clock_t t_start = clock();
178

    
179
    // Loop using sin
180
    for (int i = 0; i < num; ++i) {
181
        double arg    = double(i);
182
        double result = std::sin(arg);
183
    }
184

    
185
    // Stop timer
186
    double t_elapse_sin = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
187

    
188
    // Start timer
189
    t_start = clock();
190

    
191
    // Loop using cos
192
    for (int i = 0; i < num; ++i) {
193
        double arg    = double(i);
194
        double result = std::cos(arg);
195
    }
196

    
197
    // Stop timer
198
    double t_elapse_cos = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
199

    
200
    // Start timer
201
    t_start = clock();
202

    
203
    // Loop using tan
204
    for (int i = 0; i < num; ++i) {
205
        double arg    = double(i);
206
        double result = std::tan(arg);
207
    }
208

    
209
    // Stop timer
210
    double t_elapse_tan = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
211

    
212
    // Start timer
213
    t_start = clock();
214

    
215
    // Loop using acos
216
    double arg2 = 0.798;
217
    for (int i = 0; i < num; ++i) {
218
        double arg    = double(i);
219
        double result = std::acos(arg2);
220
    }
221

    
222
    // Stop timer
223
    double t_elapse_acos = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
224

    
225
    // Dump result
226
    std::cout << "sincos:";
227
    std::cout << " sin(x)=" << t_elapse_sin;
228
    std::cout << " cos(x)=" << t_elapse_cos;
229
    std::cout << " tan(x)=" << t_elapse_tan;
230
    std::cout << " acos(x)=" << t_elapse_acos;
231
    std::cout << " sec" << std::endl;
232

    
233
    // Return
234
    return;
235
}
236

    
237

    
238
/***********************************************************************//**
239
 * @brief sqrt
240
 ***************************************************************************/
241
void sqrt(int num)
242
{
243
    // Start timer
244
    clock_t t_start = clock();
245

    
246
    // Loop using pow
247
    for (int i = 0; i < num; ++i) {
248
        double arg    = double(i);
249
        double result = std::sqrt(arg);
250
    }
251

    
252
    // Stop timer
253
    double t_elapse_sqrt = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
254

    
255
    // Dump result
256
    std::cout << "sqrt:";
257
    std::cout << " sqrt(x)=" << t_elapse_sqrt;
258
    std::cout << " sec" << std::endl;
259

    
260
    // Return
261
    return;
262
}
263

    
264

    
265
/***********************************************************************//**
266
 * @brief log
267
 ***************************************************************************/
268
void log(int num)
269
{
270
    // Start timer
271
    clock_t t_start = clock();
272

    
273
    // Loop using pow
274
    for (int i = 0; i < num; ++i) {
275
        double arg    = double(i);
276
        double result = std::log10(arg);
277
    }
278

    
279
    // Stop timer
280
    double t_elapse_log10 = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
281

    
282
    // Start timer
283
    t_start = clock();
284

    
285
    // Loop using acos
286
    for (int i = 0; i < num; ++i) {
287
        double arg    = double(i);
288
        double result = std::log(arg);
289
    }
290

    
291
    // Stop timer
292
    double t_elapse_log = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
293

    
294
    // Dump result
295
    std::cout << "log:";
296
    std::cout << " log10(x)=" << t_elapse_log10;
297
    std::cout << " log(x)=" << t_elapse_log;
298
    std::cout << " sec" << std::endl;
299

    
300
    // Return
301
    return;
302
}
303

    
304

    
305
/***********************************************************************//**
306
 * @brief Main
307
 ***************************************************************************/
308
int main(void)
309
{
310
    // Set number of rounds
311
    int num = 100000000;
312
    std::cout << "Loops: " << num << std::endl;
313
    
314
    // Tests
315
    power2(num);
316
    power2p(num);
317
    multidiv(num);
318
    addsub(num);
319
    sincos(num);
320
    sqrt(num);
321
    log(num);
322

    
323
    // Exit
324
    return 0;
325
}
326