Advertisement
jacknpoe

Tempos de execução de fatorial for e recursivo

Jan 16th, 2024 (edited)
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | Source Code | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <locale.h>
  4.  
  5. #define ITERACOES 200000000
  6. #define CORTEFATORIAL 20
  7.  
  8. long fatorial_recursivo( long n) {
  9.   if(n == 0)
  10.     return 1;
  11.   else
  12.     return n * fatorial_recursivo(n-1);
  13. }
  14.  
  15. inline long fatorial_recursivo_inline( long n)
  16.     { return n ? n * fatorial_recursivo_inline(n-1) : 1; }
  17.  
  18. long fatorial_for( long n) {
  19.     long temp = 1, indice;
  20.     for( indice = 1; indice <= n; indice++) temp *= indice;
  21.     return temp;
  22. }
  23.  
  24. inline long fatorial_for_inline( long n) {
  25.     long temp = 1, indice;
  26.     for( indice = 1; indice <= n; indice++) temp *= indice;
  27.     return temp;
  28. }
  29.  
  30. inline long fatorial_foo( long n) {
  31.     long temp = 1, indice;
  32.     for( indice = 1; indice <= n; indice++) temp *= indice;
  33.     return temp;
  34. }
  35.  
  36. int main() {
  37.     clock_t clock1, clock2;
  38.     long acumulador, indice;
  39.  
  40.     setlocale( LC_ALL, "");     // para imprimir corretamente caracteres em português
  41.    
  42.     printf("Resolução: %d tiques por segundo.\n\n", CLOCKS_PER_SEC);
  43.     printf("Por favor, configure o ambiente antes de continuar e aperte enter.\n");
  44.     getchar();
  45.     printf("\n");
  46.  
  47.     // Fatorial Foo para evitar algum desvio de performance
  48.     acumulador = 0;
  49.     clock1 = clock();
  50.     for( indice = 0; indice < ITERACOES; indice++)
  51.         acumulador += fatorial_foo( indice % CORTEFATORIAL);
  52.     clock2 = clock();
  53.  
  54.     printf("Fatorial foo: %1.3fs (%d)\n", ((double) clock2 - clock1 ) / CLOCKS_PER_SEC, acumulador);
  55.  
  56.     // Fatorial Recursivo
  57.     acumulador = 0;
  58.     clock1 = clock();
  59.     for( indice = 0; indice < ITERACOES; indice++)
  60.         acumulador += fatorial_recursivo( indice % CORTEFATORIAL);
  61.     clock2 = clock();
  62.  
  63.     printf("Fatorial recursivo: %1.3fs (%d)\n", ((double) clock2 - clock1) / CLOCKS_PER_SEC, acumulador);
  64.  
  65.     // Fatorial Recursivo Inline
  66.     acumulador = 0;
  67.     clock1 = clock();
  68.     for( indice = 0; indice < ITERACOES; indice++)
  69.         acumulador += fatorial_recursivo_inline( indice % CORTEFATORIAL);
  70.     clock2 = clock();
  71.  
  72.     printf("Fatorial recursivo inline: %1.3fs (%d)\n", ((double) clock2 - clock1) / CLOCKS_PER_SEC, acumulador);
  73.  
  74.     // Fatorial For
  75.     acumulador = 0;
  76.     clock1 = clock();
  77.     for( indice = 0; indice < ITERACOES; indice++)
  78.         acumulador += fatorial_for( indice % CORTEFATORIAL);
  79.     clock2 = clock();
  80.  
  81.     printf("Fatorial for: %1.3fs (%d)\n", ((double) clock2 - clock1) / CLOCKS_PER_SEC, acumulador);
  82.  
  83.     // Fatorial For Inline
  84.     acumulador = 0;
  85.     clock1 = clock();
  86.     for( indice = 0; indice < ITERACOES; indice++)
  87.         acumulador += fatorial_for_inline( indice % CORTEFATORIAL);
  88.     clock2 = clock();
  89.  
  90.     printf("Fatorial for inline: %1.3fs (%d)\n", ((double) clock2 - clock1 ) / CLOCKS_PER_SEC, acumulador);
  91.  
  92.     printf("\n");
  93.     getchar();
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement