Thread: Timing a for loop

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

    Timing a for loop

    Code:
     am trying to time a for loop for different values but I can't get my for loop to stop and with the timing function I am using is it measuring in milliseconds or something else?
    #include< iostream >
    #include< iomanip >
    #include< cstdlib >
    #include< ctime >
    #include< cstdio >
    #include< fstream >
    
    const int vSize = 1;
    using namespace std;
    
    
    
    int main()
    {
       
        
         
       
        
         //Random numbers to be sorted
        int vec[vSize]={50};//,50000//,100000,200000,400000};
    
        //Declare the clock variables
        clock_t start,finish;
        clock_t startAll;
    
    
    
        
         //Declare and initialize all the variables
       double duration;
        
        //Declare the streams and the input and output files
        ifstream inFile;
       ofstream outFile;
    
        inFile.open("input1.txt");
       outFile.open("output1.txt");
    
    int Sum=0;
        //Test the algorithms
        start=clock();
        outFile<<"Numbers to be sorted"<< endl;
             for(int i =0; i < vec[vSize]; i++)
             {
                  Sum++;
                  cout<< Sum;
             }
    
             
    
    
    
    
    
        
    
    
    
              
              
              
              //Switch statement to start the different sort algorithms
             
    
             finish=clock();
             duration=(double)(finish-start)/CLOCKS_PER_SEC;
             cout<<"          "<< setw(4)<< duration;
             
        
              finish=clock();
             duration=(double)(finish-start)/CLOCKS_PER_SEC;
             cout<<"Total Duration"<< duration<< endl;
        return 0;
    }//end of main

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

    question

    Code:
    how come when I code it this way it only loops to the 2nd value in the array?
    
    #include< iostream >
    #include< iomanip >
    #include< cstdlib >
    #include< ctime >
    #include< cstdio >
    #include< fstream >
    
    const int vSize = 2;
    using namespace std;
    
    
    
    int main()
    {
       
        
         
       
        
         
        int vec[vSize]={500,200};//,50000//,100000,200000,400000};
    
        //Declare the clock variables
        clock_t start,finish;
        clock_t startAll;
    
    
    
        
         //Declare and initialize all the variables
       double duration;
        
        //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;
        //Test the algorithms
        start=clock();
        outFile<<"Numbers to be sorted"<< endl;
             for(int i =0; i < vec[vSize-1] ; i++)
             {
                  Sum++;
                  outFile<< Sum<<endl;
             }
    
             
    
    
    
    
    
        
    
    
    
              
              
              
              //Switch statement to start the different sort algorithms
             
    
             finish=clock();
             duration=(double)(finish-start)/CLOCKS_PER_SEC;
             cout<<"          "<< setw(4)<< duration;
             
        
              finish=clock();
             duration=(double)(finish-start)/CLOCKS_PER_SEC;
             cout<<"Total Duration"<< duration<< endl;
        return 0;
    }//end of main

  3. #3
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    I can't seem to keep my eyes open... aw 5 o'clock in the morning...

    this piece of code will have to do for now, maybe it's a little help.

    It's a class from my renderengine that initializes and utilizes a high precision timer if the hardware supports it, otherwise it uses a lousy multimedia timer...
    [code]

    your code here....

    [/code]

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >>for(int i =0; i < vec[vSize]; i++)

    That looks pretty suspect. Perhaps you meant:

    >> for(int i =0; i < vSize; i++)

    Still, doesn't look like much of a sorting algorithm.

    clock() returns the time in milliseconds, so by dividing by CLOCKS_PER_SEC (ie:1000) would return a number in seconds much too large to tell you anything useful. In other words, don't divide.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    In this line of code (in your first post):
    Code:
    for(int i =0; i < vec[vSize]; i++)
    vSize = 1, but your int array holds only one element, which is at position 0. vec[0] is what you want, although in this case you don't really need an array.

    In your second post, you use vec[vSize-1], which in this case is always 1. vec[1] points at the 2nd value in the array.

    So basically you need to remember that array indexes are zero based. Don't ever use the size as an index to an array, and if you want all elements start your index at 0.

    Finally, if you want to run a loop for each number in your vec array, you'll probably want to do 2 loops, with the new outer loop being the index to vec.

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

    How would you code the outer loop

    How would you code the outer loop?

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    In pseudo code:
    Code:
    for int vecIndex = 0 to (vSize - 1) do:
        for int i = 1 to vec[vecIndex] do:
            Sum++ ... whatever ...
    Notice how the inner loop counts from 0 to whatever value is in the vec array at the current index, and the outer loop makes sure that happens for each value in the vec array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timing basic operations in C++
    By StevenGarcia in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2007, 02:10 AM
  2. Performance Timing Function
    By rosicky2005 in forum C++ Programming
    Replies: 11
    Last Post: 05-31-2007, 03: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, 12:43 AM