Thread: std::clock() different process

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    std::clock() different process

    I'm using std::clock() in two instances of the same program in Windows Vista. One instance is giving normal behavior, and the other is giving crap data. Could this be my system's implementation, or is something suspect in this code:
    Code:
    //Runs in two programs -- the one I opened first behaves, the second does not
    stopwatch timer;
    timer.start();
    some_long_function();
    timer.stop();
    std::cout << timer.read() << std::endl;
    Code:
    class stopwatch
    {
    	public:
    		void start();
    		void stop();
    		const bool running() const { return rng; }
    		const unsigned long long int read();
    		static const bool auto_start = true;
    		stopwatch(bool auto_start_ = false) : curr(0), data(0)
    		{
    			initialize_data();
    			if(auto_start_ == auto_start) start();
    		}
    		~stopwatch();
    	private:
    		void initialize_data();
    		void calc_diff();
    		void* data;
    		unsigned long long int curr;
    		bool rng;
    };
    Code:
    #include "stopwatch.hpp"
    #include <ctime>
    #include <cassert>
    
    void stopwatch::start()
    {
    	if(rng || (data == 0))
    		return;
    	((std::clock_t*)data)[0] = std::clock();
    	rng = true;
    }
    
    void stopwatch::stop()
    {
    	if(!rng || (data == 0))
    		return;
    	((std::clock_t*)data)[1] = std::clock();
    	rng = false;
    	calc_diff();
    }
    
    void stopwatch::calc_diff()
    {
    	if(rng || (data == 0))
    		return;
    	std::clock_t & beg = ((std::clock_t*)data)[0];
    	std::clock_t & end = ((std::clock_t*)data)[1];
    	curr = end - beg;
    }
    
    const unsigned long long int stopwatch::read() //if rng, read() will give a current reading, if !rng, it'll return curr.
    {
    	if(!rng) return curr;
    	std::clock_t now = std::clock();
    	std::clock_t & beg = ((std::clock_t*)data)[0];
    	return now - beg;
    }
    
    void stopwatch::initialize_data()
    {
    	assert(data == 0);
    	data = new std::clock_t[2];
    }
    
    stopwatch::~stopwatch()
    {
    	if(data)
    		delete [] (std::clock_t*)(data);
    	data = 0;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you've started two copies of the same data (but you won't tell us how), that each do something (but you won't tell us what), and one of the data values you believe (but you won't tell us why) while the other one you don't (but you won't tell us what you get differently or why that can't be the right answer).

    Since we appear to have no actual information at all, that means we have to guess. Fortunately, I like guessing: I claim that your really long function requires opening a file, and the second instance of the code is unable to open it (since the first one already has) and so it quickly bails while the first instance runs to completion.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I'm not sure I understand how I haven't given enough information. What does it matter what some_long_function() does? If it fails immediately, then I'd expect to get something close to zero.

    I have two identical programs that do essentially what's in my first block of code. Two executable files. I run one and then quickly run the other. The first one eventually prints this:
    Code:
    97222
    The second takes about the same amount of time to run but displays this:
    Code:
    18446744073699396622
    Now I revised the programs to perform several calculations, and display the time for each. In each case, the second program's output seems to be about 18446744073699300000 more than the first.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What color is the sky in your world, where "it's always N larger than the first answer" is not more informative than "the second answer is crap"? Especially when N = 2^64, which it appears to be in this case.

    Anyway, if clock is unable or unwilling to return a valid answer, it will return (clock_t)(-1). So check for that.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    ah. Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM