PDA

View Full Version : سوال: نحوه محاسبه زمان اجرای برنامه



tanha2013
شنبه 04 آبان 1392, 12:55 عصر
سلام.من یه سوال داشتم
چطوری میشه زمان اجرای یه برنامه رو در همون کد محاسبه کنیم و بعد از اینکه برنامه اجراش تموم شد زمان دقیقش رو به ما اعلام کنه:
مثلا واسه برج هانوی




include <iostream.h>
#include <conio.h>


void Move(int n, char source, char destination, char spare);
int count = 0;

int main()
{
const char PEG1 = 'A', // the three pegs
PEG2 = 'B',
PEG3 = 'C';

cout << "This program solves the Hanoi Towers puzzle.\n\n";

cout << "Enter the number of disks: ";
int numDisks; // the number of disks to be moved
cin >> numDisks;
cout << endl;

Move(numDisks, PEG1, PEG2, PEG3); // the solution
// getch();
return 0;

}

/

void Move(int n,
char source, char destination, char spare)
{
if (n <= 1) // anchor
cout << "Move the top disk from " << source
<< " to " << destination << endl;
else
{ // inductive case
Move(n-1, source, spare, destination);
Move(1, source, destination, spare);
Move(n-1, spare, destination, source);
}
}

pouralijan
شنبه 04 آبان 1392, 20:31 عصر
سلام خسته نباشید
ابتدا هدر تایمر رو اضافه کنید

#include <time.h>


بعد دو تا متغیر تایمر بسازید بعد یکی رو ابتدای عملیات و دومی رو انتهای آن مقدار دهی کنید و تفاضل آن دو یه عدد به ثانیه میشه


time_t t1,t2;
double s;
time(&t1);
for(int i=0;i<1000000;i++)
cout << i <<endl;
time(&t2);
s=t2-t1;
cout << s ;