clock() questions!!!

I would like to determine how long it takes a stack class to "push" elements onto a stack.

I am trying to use the "clock()", but I am unable to get the time to accurately depict how long each push takes.

I am hoping that it shows a steady slope with each increase in the number of items pushed onto the stack...

Any and all help is welcome.

*notes
_____________
*minsize is the minimum size of the stack
*maxsize is the maximum size of the stack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

            int j = minsize;
            int count = 0; //may not be used but may be useful at some point
            for(int i = minsize; i < maxsize; i = i + stepsize){
                stack<int> mys;              
                clock_t start1 = clock();
                for(int x = 0; x < j; x++){
                    mys.push(x);
                }
                clock_t mystacktdif = clock() - start1;
                mystackt = (float)(mystacktdif) * 1000.0 / (float) CLOCKS_PER_SEC;
                cout << fixed << i << ":\t" << setprecision(3) <<  mystackt;

                j++;
                count++;
           }
Last edited on
The resolution of std::clock() is not very high.
Use a large number of pushes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <string>
#include <iostream>
#include <stack>
#include <ctime>

int main( int argc, char* argv[] )
{
    if( argc == 2 )
    {
        try
        {
            const std::size_t N = std::stoi( argv[1] ) ;

            {
                const auto n = std::clock() ;
                while( n == std::clock() ) ;
                std::cout << "N == " << N << "  true resolution: " 
                          << ( std::clock() - n ) * 1000.0 / CLOCKS_PER_SEC << " millisecs    "  ;
            }

            std::stack<int> stk  ;

            const auto start = std::clock() ;
            for( std::size_t i = 0 ; i < N ; ++i ) stk.push(i) ;
            const auto end = std::clock() ;

            std::cout << "total time: " << ( end - start ) * 1000.0 / CLOCKS_PER_SEC << " millisecs\n"
                      <<   "\tamortised time to push one item: "
                      << ( ( end - start ) * 1'000'000'000.0 / CLOCKS_PER_SEC ) / N << " nanosecs\n\n" ;
        }
        catch( const std::exception& ) { return 1 ; }
    }
} 

clang++ -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ 
./a.out 5000000 && ./a.out 25000000 && ./a.out 100000000 && ./a.out 200000000

N == 5000000  true resolution: 10 millisecs    total time: 70 millisecs
	amortised time to push one item: 14 nanosecs

N == 25000000  true resolution: 10 millisecs    total time: 350 millisecs
	amortised time to push one item: 14 nanosecs

N == 100000000  true resolution: 10 millisecs    total time: 1380 millisecs
	amortised time to push one item: 13.8 nanosecs

N == 200000000  true resolution: 10 millisecs    total time: 2800 millisecs
	amortised time to push one item: 14 nanosecs

http://coliru.stacked-crooked.com/a/f1c23f1817a7b627
Thanks JLBorges...I will try this...

*Note I did actually realize that the max amount of pushes in my program is limited to about 500....and then (using ubuntu) I get a segfault...

So, I can't actually push that much which means that I am somewhat limited...

Last edited on
> the max amount of pushes in my program is limited to about 500.

To time a large number of pushes:

a. create a stack of a type which has a trivial destructor (say stack<int> mys; )
b. push in multiples of max stack size
c. after max stack size items have been pushed, reset the stack to zero size

1
2
3
4
5
6
7
8
9
10
11
const std::size_t NPUSHES = 100000000 ; // integral multiple of MAXSZ
const std::size_t MAXSZ = 500 ;
const std::size_t NCHUNKS = NPUSHES / MAXSZ ;

stack<int> mys;

for( std::size_t i = 0 ; i < NCHUNKS ; ++i ) 
{
    for( std::size_t i = 0 ; i < MAXSZ ; ++i ) mys.push(i) ;
    mys.clear() ; // reset to zero size
}
What I'm actually trying to do is figure out how "clock()" can accurately time the amount of time it takes to "push" in the amount of elements during each loop from "minsize" to "maxsize"

Once I know how to properly use the "clock()" function, I will then compare the "push" times of the STL stack with my own version of the stack class....

Where I am running into issues is I am not able to properly determine the amount of time it is taking for STL stack to "push" in the amount of elements.

Simply put...I know that I can put clock() start before I begin the "push" for loop, and I know I can put clock() end after the for loop.

But I need help with figuring out how to find how much time it takes for each loop of "push" from minsize to maxsize...

for example: I need to know how long it takes to push 100 elements first...then 101 elements...and then 102 elements...and then 103 elements...and then 104 elements...etc...all the way to maxsize.

Your first solution seems like it will work, but it will take some tweaking for my specific example..so I will try and see what I can come up with.

I really appreciate your help btw!

Do you think this is an accurate measurement over the for loop?
My code now:
----------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
            int j = minsize;
            int count = 0;
            for(int i = minsize; i < maxsize; i = i + stepsize){

                stack<int> s;              
                clock_t start1 = clock();

                for(int x = 0; x < j; x++){
                    s.push(x);
                    //cout<<"s size: " << mys.size() << endl;
                }
                clock_t stacktdif = clock();
                stackt = (float)(stacktdif - start1) * 1000.0 / (float) CLOCKS_PER_SEC;
                stacktsum+= stackt;
                cout << fixed << i << ":\t" << setprecision(3) <<  stacktsum;

            }
Last edited on
Topic archived. No new replies allowed.