diff --git a/Principal.c b/Principal.c new file mode 100644 index 0000000..f64eaaa --- /dev/null +++ b/Principal.c @@ -0,0 +1,76 @@ +#include +#include +#include + + +#include"Sequences.h" +#include"files.h" + +#define AMOUNT 30 +#define INDEX 0 +#define ITERATIVE 1 +#define RECURSIVE 2 +#define STATISTIC 1000.0 +#define COLUMNS 3 + +int main(void) +{ + long long int number; + size_t index; + long long int result = 0; + clock_t start, stop; + double cpu_time = 0; + size_t statistic = 0; + + long double buffer_matrix[AMOUNT][COLUMNS] = {0}; + + FILE * Archivo = NULL; + + Archivo = file_new("TIME_FIBONACCI.dat","w"); + + for(index = 0;index < AMOUNT;index++) + { + printf("%zu\n",index+1); + + /*Realizamos el calculo iterativamente*/ + start = clock(); + for (statistic = 0; statistic < STATISTIC; statistic ++){ + result = Sequences_sfibo(index); + + } + stop = clock(); + printf("Result: %lld ", result); + + cpu_time = ((double)(stop-start)) / CLOCKS_PER_SEC/STATISTIC; + + /*Guardamos el los valores en la matriz*/ + buffer_matrix[index][INDEX] = index; + buffer_matrix[index][ITERATIVE] = cpu_time*1e3; + + printf("\nSequential Fibo Time: %f ms\n",cpu_time*1e3); + + + /*Realizamos el calculo recursivamente*/ + start = clock(); + Sequences_rfibo(index); + stop = clock(); + + cpu_time = ((double)(stop-start))/CLOCKS_PER_SEC; + + /*Guardamos el los valores en la matriz*/ + buffer_matrix[index][RECURSIVE] = cpu_time; + + printf("Recursive Fibo Time: %f s\n",cpu_time); + + printf("\n\n"); + + } + + /*Para terminar, llamamos a nuestra funcion que escribira la matriz en el archivo.*/ + file_num_write(Archivo,COLUMNS,buffer_matrix,AMOUNT); + + printf("\n"); + + return 0; + +} diff --git a/Sequences.c b/Sequences.c new file mode 100644 index 0000000..dd85bdd --- /dev/null +++ b/Sequences.c @@ -0,0 +1,53 @@ +// Sequences.c +// +// +// Created by Rodrigo Garcia and Jesus Dominguez on 09/09/2020. +// + +#include"Sequences.h" + +/** + * Returns the value of the fibonacci sequence at index n calculated sequentially + * @param + * n (long long int): + * Index of the fibonacci sequence + * @return long long int value +*/ + +long long int Sequences_sfibo(long long int n) +{ + long long int i=0, j=1, temp; + long long int count; + + for(count = 0;count < n;count++) + { + temp = i + j; + i = j; + j = temp; + } + + return i; + +} + + +/** + * Returns the value of the fibonacci sequence at index n calculated recursively + * @param + * n (long long int): + * Index of the fibonacci sequence + * @return long long int value +*/ +long long int Sequences_rfibo(long long int n) +{ + if(n < 2) + { + return n; + } + + else + { + return Sequences_rfibo(n-2) + Sequences_rfibo(n-1); + } + +} diff --git a/TIME_FIBONACCI.dat b/TIME_FIBONACCI.dat new file mode 100644 index 0000000..7800176 --- /dev/null +++ b/TIME_FIBONACCI.dat @@ -0,0 +1,30 @@ +0 0.000009 0.000000 +1 0.000011 0.000000 +2 0.000014 0.000001 +3 0.000018 0.000001 +4 0.000024 0.000001 +5 0.000023 0.000001 +6 0.000031 0.000001 +7 0.000113 0.000001 +8 0.000041 0.000001 +9 0.000095 0.000002 +10 0.000043 0.000002 +11 0.000078 0.000001 +12 0.000052 0.000002 +13 0.000055 0.000003 +14 0.000058 0.000005 +15 0.000063 0.000007 +16 0.000067 0.000011 +17 0.000071 0.000017 +18 0.000075 0.000028 +19 0.000079 0.000056 +20 0.000113 0.000094 +21 0.000099 0.000176 +22 0.000111 0.000308 +23 0.000110 0.000292 +24 0.000114 0.000493 +25 0.000118 0.000870 +26 0.000122 0.001353 +27 0.000241 0.003185 +28 0.000149 0.004262 +29 0.000134 0.005731 diff --git a/files.c b/files.c new file mode 100644 index 0000000..d6d5f21 --- /dev/null +++ b/files.c @@ -0,0 +1,58 @@ +#include"files.h" + + +/** + * Instanciates a new file pointer identified via its name and mode. + * @param + * name (char *): + * Name of the file + * mode (char *): + * Mode of file to be opened {r, rb, a, ab, w, wb} + * + * @return FILE * opened_file. +*/ + +FILE * file_new(char *name, char *mode) +{ + FILE * file; + + file = fopen(name,mode); + + return file; +} + + +/** + * Writes a bidimensional buffer array into a file. + * @param + * file (FILE *): + * Storage file + * columns (size_t ): + * length of the buffer array + * buff [ ][columns] (long double): + * RAM matrix + * rows (size_t): + * width of the storage buffer + * + * @return void. +*/ + +void file_num_write(FILE * file, size_t columns, long double buff[] [columns], size_t rows) +{ + int count_rows,count_columns=0; + + for(count_rows = 0;count_rows < rows;count_rows++) + { + fprintf(file,"%.0Lf ",buff[count_rows][count_columns]); + count_columns++; + fprintf(file,"%Lf ",buff[count_rows][count_columns]); + count_columns++; + fprintf(file,"%Lf",buff[count_rows][count_columns]); + + count_columns=0; + + fprintf(file,"\n"); + } + + fclose(file); +} diff --git a/test b/test new file mode 100755 index 0000000..74ba83f Binary files /dev/null and b/test differ