-
Notifications
You must be signed in to change notification settings - Fork 3
/
app2.C
74 lines (58 loc) · 1.89 KB
/
app2.C
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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
int64_t numIterations = 0;
int Calc1 (const int num)
{
static int result = 0;
//for (int i = 0; i < num; i++)
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 5; j++)
{
result += (i*i);
}
numIterations++;
}
return result;
}
int main (int argc, char* argv[])
{
//const int startValue = atoi(argv[1]);
//int value = int(char(argv[0][0]));
int value = 10;
int64_t maxIterations = int64_t(100) * 1000 * 1000;
if (argc > 1)
{
maxIterations = int64_t( atoi(argv[1]) ) * 1000 * 1000;
}
printf("started with value %d, for %lld iterations; PID: %d\n", value, maxIterations, getpid());
struct timeval startTime, endTime;
while (1)
{
//const time_t startTime = time(NULL);
gettimeofday(&startTime, NULL);
const int64_t startIterations = numIterations;
value = Calc1(value);
//const time_t endTime = time(NULL);
gettimeofday(&endTime, NULL);
const int64_t startUsec = (int64_t(startTime.tv_sec)*1000*1000) + int64_t(startTime.tv_usec);
const int64_t endUsec = (int64_t(endTime.tv_sec) *1000*1000) + int64_t(endTime.tv_usec);
const int64_t durationUsec = endUsec - startUsec;
const int64_t deltaIterations = numIterations - startIterations;
const double iterationsPerSecond = double(deltaIterations) / (double(durationUsec) / (1000*1000));
printf("\rit/s: %f (%lld it in %lld usec) ", iterationsPerSecond, deltaIterations, durationUsec);
if ( /*value > 100 * 1000 * 1000 ||*/ value <= 0)
{
value = 10;
}
//usleep(10000);
// if (maxIterations > 0 && numIterations > maxIterations)
// {
// break;
// }
}
return value;
}