-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10.c
More file actions
62 lines (50 loc) · 1.32 KB
/
10.c
File metadata and controls
62 lines (50 loc) · 1.32 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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define TRY 100000
double timeSec() {
struct timespec tv;
clock_gettime(CLOCK_MONOTONIC, &tv);
return tv.tv_sec + ((double) tv.tv_nsec) / 1000000000;
}
int nodI(int a, int b) {
int c = 1;
while (b != 0) {
int c = a % b;
a = b;
b = c;
}
return a;
}
int nodR(int a, int b) {
if (b == 0) return a;
nodR(b, a % b);
}
int main(int argc, char const *argv[]) {
printf("Print two number:\n");
int a, b;
scanf("%d%d", &a, &b);
double timeRS, timeRF, timeIS, timeIF;
int nodr, nodi;
timeRS = timeSec();
for (size_t i = 0; i < TRY; i++) {
nodr = nodR(a, b);
}
timeRF = timeSec();
timeIS = timeSec();
for (size_t i = 0; i < TRY; i++) {
nodi = nodI(a, b);
}
timeIF = timeSec();
if (nodi != nodr) {
printf("--------------------------------------------------------------\n");
printf("%14s | %6d %6d | \nIteractive : %6d\nRecursive : %6d\n", "ERROR NOD", a, b, nodi, nodr);
return 1;
}
printf("-------------------------------------------------\n");
printf("%14s | %6.15lf sec\n%14s | %6.15lf sec\n",
"Recursive", (timeRF - timeRS) / TRY,
"Normal", (timeIF - timeIS) / TRY);
return 0;
}