The Timing is incorret

This is a discussion on The Timing is incorret within the C++ Programming forums, part of the General Programming Boards category; All these times should be the same: Total Time Duration0.841 ms Total Time Duration2.384 ms Total Time Duration0.4 ms Total ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    The Timing is incorret

    All these times should be the same:
    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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    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
    Last edited by Lynux-Penguin; 08-28-2003 at 04:00 PM.
    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

  4. #4
    *******argv[] - hu? darksaidin's Avatar
    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]

  5. #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

  6. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming Timing
    By CFun in forum C Programming
    Replies: 6
    Last Post: 02-25-2008, 10:16 AM
  2. Performance Timing Function
    By rosicky2005 in forum C++ Programming
    Replies: 11
    Last Post: 05-31-2007, 04:09 PM
  3. My Timing System
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 01-01-2006, 11:43 PM
  4. Games - timing
    By Magos in forum Game Programming
    Replies: 7
    Last Post: 03-06-2004, 11:32 AM
  5. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 01:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21