Execution speed test in c++

This is a discussion on Execution speed test in c++ within the C++ Programming forums, part of the General Programming Boards category; i have a c++ and java program which both work. I know what i need to do in java now ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    Execution speed test in c++

    i have a c++ and java program which both work. I know what i need to do in java now but not c++. The program gets the work done, but how can i time it? All examples i seem to be getting to test the speed are unix specific. This is my program
    Code:
    #include "stdafx.h"
    #include <set>
    #include <iostream>
    
    using namespace std;
    
    class RInteger {
    private:
            int i;
    public:
            RInteger(int v) { i = v; }
            friend ostream& operator <<(ostream& o, const RInteger &ri);
            inline friend bool operator <(const RInteger a, const RInteger b);
    };
    
    ostream&
    operator <<(ostream& o, const RInteger &ri)
    {
            cout << ri.i;
            return o;
    }
    
    inline bool
    operator <(const RInteger a, const RInteger b)
    {
            return (b.i < a.i);
    }
    
    int main(int argc, char *argv[])
    {
            set <RInteger> s;
            int i;
            int rand = 10;
    
            for (i = 0; i < 1000000; i++) {
                    s.insert(RInteger(rand));
                    rand = rand * 1103515245 + 12345;
            }
            set<RInteger>::const_iterator si;
            for (i = 0, si = s.begin(); si != s.end(); i++, si++)
                    if (i % 100000 == 0)
                            cout << *si << '\n';
            return 0;
    }
    So somehow i need to print out the starting time, and the ending time of this program. How would i add this to my program which would work? (using windows vista, visual c++ 2008).
    cheers

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You could use standard C functions in time.h like time() or clock(), but they probably aren't fast enough for your purposes.

    In that case, I will suggest this solution. It's accurate to a few milliseconds, but it's Windows-specific. Displaying Function Run Time
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed differences whilst inside the IDE
    By deviousdexter in forum C# Programming
    Replies: 4
    Last Post: 12-04-2008, 12:59 PM
  2. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  3. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  4. Test my new tetris game
    By PJYelton in forum Game Programming
    Replies: 6
    Last Post: 04-19-2003, 05:21 PM
  5. controlling game speed
    By m00se123 in forum Game Programming
    Replies: 5
    Last Post: 05-29-2002, 01:38 PM

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