![]() |
| | #1 |
| Registered User Join Date: Aug 2001
Posts: 84
| The Timing is incorret Total Time Duration0.841 ms Total Time Duration2.384 ms Total Time Duration0.4 ms Total Time Duration0.411 ms Total Time Duration0.411 ms Total Time Duration0.55 ms Total Time Duration0.411 ms Total Time Duration0.401 ms Total Time Duration0.41 ms Press any key to continue What am I doing wrong? Code: #include< iostream >
#include< iomanip >
#include< cstdlib >
#include< ctime >
#include< cstdio >
#include< fstream >
const int vSize = 3;
using namespace std;
int main()
{
int vec[vSize]={5000, 5000, 5000};//,50000//,100000,200000,400000};
//Declare the clock variables
clock_t start,finish, start1, finish1, start2, finish2;
clock_t startAll;
//Declare and initialize all the variables
double duration, duration1, duration2;
//Declare the streams and the input and output files
ifstream inFile;
ofstream outFile;
inFile.open("input1.txt");
outFile.open("A:/output1.txt");
int Sum=0;
for (int iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
{
start=clock();
for(int i =0; i < vec[iterationcountselector] ; i++)
{
Sum++;
outFile<< Sum<<endl;
}
/* finish=clock();
duration=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<" "<< setw(4)<< duration;*/
finish=clock();
duration=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"Total Time Duration"<< duration<<" ms"<< endl;
}
for (iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
{
start1=clock();
for(int i =0; i < vec[iterationcountselector]; i++)
{
Sum++;
outFile<< Sum<<endl;
}
finish1=clock();
duration1=(double)(finish1-start1)/CLOCKS_PER_SEC;
cout<<"Total Time Duration"<< duration1<<" ms"<< endl;
}
for (iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
{
start2=clock();
for(int i =0; i < vec[iterationcountselector]; i++)
{
Sum++;
outFile<< Sum<<endl;
}
finish2=clock();
duration2=(double)(finish2-start2)/CLOCKS_PER_SEC;
cout<<"Total Time Duration"<< duration2<<" ms"<< endl;
}
return 0;
}//end of main
|
| Drew is offline | |
| | #2 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
| What makes you think that your process has 100% of the CPU time during the entire time the program is running? Operating systems may interupt a program during its run to perform necessary tasks. When the OS is done with whatever it needed to do, the program will be restarted. This happens many times each second but it is transparent to you the user. Depending on where in your programs execution the OS decided to switch to another task and what specific tasks the OS needed to accomplish at that time, your timing values are bound to differ by varying amounts as is apparent in your results mentioned. For all of those loops to perform in the exact same amount of time, you would at least need to ensure that your program would be the only thing running from start to finish.
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #3 |
| Comment your source code! Join Date: Apr 2002
Posts: 533
| and (if what I remember is correct) interrupt signals are sent to your program to pause it and make it wait for the devices to continue. I believe they are known as IRQs. (or its the other way around when the software sends an IRQ to the hardware, I forgot...) edit: ok just checked it out: IRQs are sent form the hardware to the CPU so the CPU stops communication with the program (for like a nanosec) and talks with the hardware etc. -LC
__________________ Asking the right question is sometimes more important than knowing the answer. Please read the FAQ C Reference Card (A MUST!) Pointers and Memory The Essentials CString lib Last edited by Lynux-Penguin; 08-28-2003 at 03:00 PM. |
| Lynux-Penguin is offline | |
| | #4 |
| *******argv[] - hu? Join Date: Jul 2003
Posts: 314
| >>Total Time Duration2.384 ms >>Total Time Duration0.4 ms You can't legally call that "variation", Salem ![]() It's off by 600%.
__________________ [code] your code here.... [/code] |
| darksaidin is offline | |
| | #5 |
| Registered User Join Date: Aug 2001
Posts: 84
| I got it figured out this is what the teacher wanted. Code: #include< iostream >
#include< iomanip >
#include< ctime >
#include< fstream >
const int vSize = 6;
using namespace std;
int main()
{
int vec[vSize]={300, 500, 700,900, 1200, 1300};
//Declare the clock variables
clock_t start,finish, start1, finish1, start2, finish2, start3, finish3,
start4, finish4, start5, finish5;
// clock_t startAll;
//Declare and initialize all the variables
double duration, duration1, duration2, duration3, duration4, duration5=0;
//Declare the streams and the input and output files
ofstream outFile;
ofstream outFile2;
outFile.open("output1.txt");
outFile2.open("output2.txt");
outFile2<<"Chapter 2 Problem 7b Implement the code in the language"<<endl;
outFile2<<"of your choice, and give the running time for several values of N"<<endl<<endl;
int Sum=0;
outFile2<<"For the for loop: for(i = 0; i < n; i++)"<<endl<<endl;
for (int iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
{
start=clock();
for(int i =0; i < vec[iterationcountselector] ; i++)
{
Sum++;
outFile<< Sum<<endl;
}
finish=clock();
duration=(double)(finish-start)/CLOCKS_PER_SEC;
outFile2<<"Total Time Duration for vector:"<<vec[iterationcountselector]<<" "<< duration<<" ms"<< endl;
}
outFile2<<endl<<endl;
///////////////////////////////////////////////////////////////////////
outFile2<<"For the loops:for(int i =0; i < vec[iterationcountselector]; i++)"<<endl;
outFile2<<" for(int j = 0; j < vec[iterationcountselector]; j++)"<<endl<<endl;
Sum=0;
for (iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
{
start1=clock();
for(int i =0; i < vec[iterationcountselector]; i++)
{
for(int j = 0; j < vec[iterationcountselector]; j++)
Sum++;
outFile<< Sum<<endl;
}
finish1=clock();
duration1=(double)(finish1-start1)/CLOCKS_PER_SEC;
outFile2<<"Total Time Duration for vector:"<<vec[iterationcountselector]<<" "<<duration1<<" ms"<< endl;
}
outFile2<<endl<<endl;
//////////////////////////////////////////////////////////////////////
outFile2<<"For the loops:for(int i =0; i < vec[iterationcountselector]; i++)"<<endl;
outFile2<<" for(int j = 0; j < vec[iterationcountselector] * vec[iterationcountselector]; j++)"<<endl<<endl;
Sum=0;
for (iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
{
start2=clock();
for(int i =0; i < vec[iterationcountselector]; i++)
{
for(int j = 0; j < vec[iterationcountselector] * vec[iterationcountselector]; j++)
Sum++;
outFile<< Sum<<endl;
}
finish2=clock();
duration2=(double)(finish2-start2)/CLOCKS_PER_SEC;
outFile2<<"Total Time Duration for vector:"<<vec[iterationcountselector]<<" "<< duration2<<" ms"<< endl;
}
outFile2<<endl<<endl;
/////////////////////////////////////////////////////////////////////
outFile2<<"For the loops:for(int i =0; i < vec[iterationcountselector]; i++)"<<endl;
outFile2<<" for(int j = 0; j < i; j++)"<<endl<<endl;
Sum=0;
for (iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
{
start3=clock();
for(int i =0; i < vec[iterationcountselector]; i++)
{
for(int j = 0; j < i; j++)
Sum++;
outFile<< Sum<<endl;
}
finish3=clock();
duration3=(double)(finish3-start3)/CLOCKS_PER_SEC;
outFile2<<"Total Time Duration for vector:"<<vec[iterationcountselector]<<" "<< duration3<<" ms"<< endl;
}
outFile2<<endl<<endl;
////////////////////////////////////////////////////////////////////////
outFile2<<"For the loops:for(int i =0; i < vec[iterationcountselector]; i++)"<<endl;
outFile2<<" for(int j = 0; j < i; j++)"<<endl;
outFile2<<" for(int k = 0; k < j; k++)"<<endl<<endl;
Sum=0;
for (iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
{
start4=clock();
for(int i =0; i < vec[iterationcountselector]; i++)
{
for(int j = 0; j < i; j++)
for(int k = 0; k < j; k++)
Sum++;
outFile<< Sum<<endl;
}
finish4=clock();
duration4=(double)(finish4-start4)/CLOCKS_PER_SEC;
outFile2<<"Total Time Duration for vector:"<<vec[iterationcountselector]<<" "<< duration4<<" ms"<< endl;
}
outFile2<<endl<<endl;
//////////////////////////////////////////////////////////////////////
outFile2<<"For the loops:for(int i =0; i < vec[iterationcountselector]; i++)"<<endl;
outFile2<<" for(int j = 1; j < i * i; j++)"<<endl;
outFile2<<" if(j % i == 0)"<<endl;
outFile2<<" for(int k = 0; k < j; k++)"<<endl<<endl;
Sum=0;
for (iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
{
start5=clock();
for(int i =0; i < vec[iterationcountselector]; i++)
{
for(int j = 1; j < i * i; j++)
if(j % i == 0)
for(int k = 0; k < j; k++)
Sum++;
outFile<< Sum<<endl;
}
finish5=clock();
duration5=(double)(finish5-start5)/CLOCKS_PER_SEC;
outFile2<<"Total Time Duration for vector:"<<vec[iterationcountselector]<<" "<< duration5<<" ms"<< endl;
}
outFile2<<endl<<endl;
return 0;
}//end of main
|
| Drew is offline | |
| | #6 |
| Registered User Join Date: Aug 2001
Posts: 84
| here is the output Chapter 2 Problem 7b Implement the code in the language of your choice, and give the running time for several values of N For the for loop: for(i = 0; i < n; i++) Total Time Duration for vector:300 0 ms Total Time Duration for vector:400 0.01 ms Total Time Duration for vector:500 0.02 ms Total Time Duration for vector:600 0.01 ms Total Time Duration for vector:700 0.02 ms Total Time Duration for vector:800 0.03 ms For the loops:for(int i =0; i < vec[iterationcountselector]; i++) for(int j = 0; j < vec[iterationcountselector]; j++) Total Time Duration for vector:300 0 ms Total Time Duration for vector:400 0.01 ms Total Time Duration for vector:500 0.02 ms Total Time Duration for vector:600 0.02 ms Total Time Duration for vector:700 0.03 ms Total Time Duration for vector:800 0.03 ms For the loops:for(int i =0; i < vec[iterationcountselector]; i++) for(int j = 0; j < vec[iterationcountselector] * vec[iterationcountselector]; j++) Total Time Duration for vector:300 0.29 ms Total Time Duration for vector:400 0.671 ms Total Time Duration for vector:500 1.302 ms Total Time Duration for vector:600 2.243 ms Total Time Duration for vector:700 3.545 ms Total Time Duration for vector:800 5.268 ms For the loops:for(int i =0; i < vec[iterationcountselector]; i++) for(int j = 0; j < i; j++) Total Time Duration for vector:300 0.01 ms Total Time Duration for vector:400 0.01 ms Total Time Duration for vector:500 0.02 ms Total Time Duration for vector:600 0.02 ms Total Time Duration for vector:700 0.02 ms Total Time Duration for vector:800 0.03 ms For the loops:for(int i =0; i < vec[iterationcountselector]; i++) for(int j = 0; j < i; j++) for(int k = 0; k < j; k++) Total Time Duration for vector:300 0.05 ms Total Time Duration for vector:400 0.1 ms Total Time Duration for vector:500 0.201 ms Total Time Duration for vector:600 0.32 ms Total Time Duration for vector:700 0.511 ms Total Time Duration for vector:800 0.741 ms For the loops:for(int i =0; i < vec[iterationcountselector]; i++) for(int j = 1; j < i * i; j++) if(j % i == 0) for(int k = 0; k < j; k++) Total Time Duration for vector:300 9.393 ms Total Time Duration for vector:400 29.423 ms Total Time Duration for vector:500 71.432 ms Total Time Duration for vector:600 148.784 ms Total Time Duration for vector:700 274.505 ms Total Time Duration for vector:800 473.17 ms |
| Drew is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C Programming Timing | CFun | C Programming | 6 | 02-25-2008 10:16 AM |
| Performance Timing Function | rosicky2005 | C++ Programming | 11 | 05-31-2007 03:09 PM |
| My Timing System | jmd15 | Windows Programming | 4 | 01-01-2006 11:43 PM |
| Games - timing | Magos | Game Programming | 7 | 03-06-2004 11:32 AM |
| Timing in Windows | steinberg | Windows Programming | 3 | 07-14-2002 12:43 AM |