-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppbench.cpp
More file actions
214 lines (202 loc) · 7.48 KB
/
cppbench.cpp
File metadata and controls
214 lines (202 loc) · 7.48 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <math.h>
#include <ctime>
#include <limits>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////////
int main( )
{
double w = 0.999999;
std::clock_t start;
double duration;
double answer;
int upperlimit;
upperlimit = 10000000;
int i;
std::clock_t programstart;
int element = 0;
int iteration = 0;
int iterations = 0;
int innerloop = 0;
double sum = 0;
int array_length = 100000000;
// Create and open a text file
ofstream MyFile("speedbench-cpp.txt");
////////////////////////////////////////////////////////////////////////
printf("C++ speed test begins.....\n");
answer=w;
programstart = std::clock();
start = std::clock();
for ( i=0;i<upperlimit+1;i++)
answer = sqrt(answer); // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("Sqrt of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << setprecision (4) << fixed;
MyFile << "Sqrt "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
answer=w;
start = std::clock();
for (int i=0;i<upperlimit+1;i++)
answer = sin(answer); // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("Sin of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "Sin "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
answer=w;
start = std::clock();
for (int i=0;i<upperlimit+1;i++)
answer = exp(-answer); // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("Exp of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "Exp "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
answer=w;
start = std::clock();
for (int i=0;i<upperlimit+1;i++)
answer = log(1.0+abs(answer)); // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("Ln of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "Ln "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
answer=w;
start = std::clock();
for (int i=0;i<upperlimit+1;i++)
answer = cos(answer); // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("Cos of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "Cos "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
answer=w;
start = std::clock();
for (int i=0;i<upperlimit+1;i++)
answer = pow(answer,4); // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("x^4 of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "x^4 "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
answer=w;
start = std::clock();
for (int i=0;i<upperlimit+1;i++)
answer = answer*w; // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("x*x of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "x*x "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
answer=w;
start = std::clock();
for (int i=0;i<upperlimit+1;i++)
answer = answer/w; // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("x/x of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "x/x "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
answer=w;
start = std::clock();
for (int i=0;i<upperlimit+1;i++)
answer = answer + w; // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("x+x of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "x+x "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
answer=w;
start = std::clock();
for (int i=0;i<upperlimit+1;i++)
answer = answer - w; // algorithm here
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("x-x of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "x-x "<< setw(5) << setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
upperlimit = 10000000;
answer = w;
start = std::clock();
for ( i=0;i<upperlimit+1;i++)
{answer = sqrt(answer);
answer = sin(answer);
answer = exp(-answer);
answer = log(1.0+abs(answer));
answer = cos(answer);
answer = pow(answer,4);
answer = answer*w;
answer = answer/w;
answer = answer+w;
answer = answer-w; }
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("Loop of %4f", w);
printf(" is %11.4e", answer);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "Loop "<< setw(6)<< setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
start = std::clock();
double *array = new double[array_length];
iterations=10;
for (element = 0; element < array_length; element++)
array[element] = element;
for (iteration = 0; iteration < iterations; iteration++)
for (innerloop = 0; innerloop < 10000000; innerloop++)
sum += array[(iteration + innerloop) % array_length];
delete array;
array = NULL;
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
printf("Array of %d", iterations);
printf(" is %11.4e ",sum);
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "Array "<< setw(6)<< setprecision(6) << duration;
MyFile << "\n";
////////////////////////////////////////////////////////////////////////
duration = ( std::clock() - programstart) / (double) CLOCKS_PER_SEC;
printf("IRun ");
printf(" Time = %10f seconds. \n", duration);
// Write to the file
MyFile << "IRun "<< setw(6) << setprecision(6) << duration;
MyFile << "\n";
// Close the file
MyFile.close();
printf("Done.\n");
}