Skip to content

Commit 413a972

Browse files
committed
feat: added time example
1 parent ceb1e41 commit 413a972

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

execution_time/fibonacci.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
4+
int fibonacci(int n) {
5+
if (n == 0) {
6+
return 0;
7+
} else if (n == 1) {
8+
return 1;
9+
} else {
10+
return fibonacci(n - 1) + fibonacci(n - 2);
11+
}
12+
}
13+
14+
int fibonacci_iterativo(int n) {
15+
int f0 = 0;
16+
int f1 = 1;
17+
int fn = 0;
18+
int i;
19+
if (n == 0) {
20+
return f0;
21+
} else if (n == 1) {
22+
return f1;
23+
} else {
24+
for (i = 2; i <= n; i++) {
25+
fn = f0 + f1;
26+
f0 = f1;
27+
f1 = fn;
28+
}
29+
return fn;
30+
}
31+
}
32+
33+
int main() {
34+
clock_t inicio = clock();
35+
int n = 40;
36+
printf("Fibonacci(%d) = %d\n", n, fibonacci(n));
37+
double tempo = (double)(clock() - inicio) / CLOCKS_PER_SEC;
38+
tempo = tempo * 1000; //milisegundos
39+
printf("Tempo de execucao: %.5f ms\n", tempo);
40+
41+
inicio = clock();
42+
printf("Fibonacci(%d) = %d\n", n, fibonacci_iterativo(n));
43+
tempo = (double)(clock() - inicio) / CLOCKS_PER_SEC;
44+
tempo = tempo * 1000; //milisegundos
45+
printf("Tempo de execucao: %.5f ms\n", tempo);
46+
return 0;
47+
}

execution_time/tempo.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
4+
int main() {
5+
clock_t inicio = clock();
6+
// Executar o algoritmo
7+
double tempo = (double)(clock() - inicio) / CLOCKS_PER_SEC;
8+
tempo = tempo * 1000; //milisegundos
9+
printf("Tempo de execucao: %.50f\n", tempo);
10+
return 0;
11+
}

0 commit comments

Comments
 (0)