vendettaaaaa
19-01-2012, 19:21
Ciao,
inizio a pensare che il mio pc abbia problemi nel chiamare le funzioni che time() e clock(). Entrambe mi restituiscono un numero assurdo di secondi.
Pensando che magari sbagliassi la sintassi, ho provato anche con una libreria fornita da un prof dell'Uni, che contiene un'analoga funzione ma il risultato è analogo (ottengo circa 68 secondi anzichè, a occhio, direi 10).
Sia su Visual Studio 2010 che sul vecchio Visual C++ 6, nella virtual machine.
Il codice è il seguente:
#include <iostream>
#include <ctime>
using namespace std;
double factorial(int n) {return (n == 1 || n == 0) ? 1. : factorial(n - 1) * n;}
void main(void)
{
int n;
cout << "Exercise 1.7: Factorial number" << endl;
do
{
cout << "\nInput a positive integer number to know its Factorial: ";
cin >> n;
} while(n < 0);
double temp;
time_t start,end;
time(&start);
for(int k = 1; k <= 10000000; k++)
{
// // Iterative version
// if(n == 0 || n == 1)
// {
//// cout << "The Factorial of " << n << " is: " << 1 << endl;
// return;
// }
//
// double result = 1.;
// for(int i = 2; i <= n; i++) result *= i;
// //cout << "The Factorial of " << n << " is: " << result << endl;
// Recursive version
temp = factorial(n);
// cout << "The Factorial of " << n << " is: " << factorial(n) << endl;
}
time(&end);
cout << "It took " << cout.precision(8) << difftime(end,start) << "s to calculate the Factorial of " << n << " for 10 million times" << endl;
return;
}
Il risultato è lo stesso usando
time_t start = clock();
{ciclo for};
time_t end = clock();
tempoTotale = (double)(end - start)/CLOCKS_PER_SEC;
Che diavolo succede?? :D
inizio a pensare che il mio pc abbia problemi nel chiamare le funzioni che time() e clock(). Entrambe mi restituiscono un numero assurdo di secondi.
Pensando che magari sbagliassi la sintassi, ho provato anche con una libreria fornita da un prof dell'Uni, che contiene un'analoga funzione ma il risultato è analogo (ottengo circa 68 secondi anzichè, a occhio, direi 10).
Sia su Visual Studio 2010 che sul vecchio Visual C++ 6, nella virtual machine.
Il codice è il seguente:
#include <iostream>
#include <ctime>
using namespace std;
double factorial(int n) {return (n == 1 || n == 0) ? 1. : factorial(n - 1) * n;}
void main(void)
{
int n;
cout << "Exercise 1.7: Factorial number" << endl;
do
{
cout << "\nInput a positive integer number to know its Factorial: ";
cin >> n;
} while(n < 0);
double temp;
time_t start,end;
time(&start);
for(int k = 1; k <= 10000000; k++)
{
// // Iterative version
// if(n == 0 || n == 1)
// {
//// cout << "The Factorial of " << n << " is: " << 1 << endl;
// return;
// }
//
// double result = 1.;
// for(int i = 2; i <= n; i++) result *= i;
// //cout << "The Factorial of " << n << " is: " << result << endl;
// Recursive version
temp = factorial(n);
// cout << "The Factorial of " << n << " is: " << factorial(n) << endl;
}
time(&end);
cout << "It took " << cout.precision(8) << difftime(end,start) << "s to calculate the Factorial of " << n << " for 10 million times" << endl;
return;
}
Il risultato è lo stesso usando
time_t start = clock();
{ciclo for};
time_t end = clock();
tempoTotale = (double)(end - start)/CLOCKS_PER_SEC;
Che diavolo succede?? :D