Clock Troubles

This is a discussion on Clock Troubles within the C Programming forums, part of the General Programming Boards category; If the whole code uses 100% CPU power then why do you assume it is from the Sleep() function? Or ...

  1. #16
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If the whole code uses 100% CPU power then why do you assume it is from the Sleep() function? Or you actually mean you tested the Sleep() function and it uses 100% CPU?
    If you haven't tested it you can put like a big value to Sleep() and make a program executing only Sleep() and see what happens. How to you see how much CPU the program uses?

    I don't know any other function btw. I would say that Sleep() shouldn't use much CPU. Judging only by the name and its description http://msdn.microsoft.com/en-us/libr...98(VS.85).aspx
    But it might not be sophisticated at all...

  2. #17
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by C_ntua View Post
    If the whole code uses 100% CPU power then why do you assume it is from the Sleep() function? Or you actually mean you tested the Sleep() function and it uses 100% CPU?
    If you haven't tested it you can put like a big value to Sleep() and make a program executing only Sleep() and see what happens. How to you see how much CPU the program uses?

    I don't know any other function btw. I would say that Sleep() shouldn't use much CPU. Judging only by the name. But it might not be sophisticated at all...
    No No No, you got me a little bit wrong. I didn't explain myself well... Sleep does not use CPU power. But the while loop
    Code:
    	while((ticks2 - ticks1) / CLOCKS_PER_SEC < 50)
    		ticks2=clock();
    which the only thing it does is wait 50 seconds, uses 100% CPU power. I'm wondering how could I write my own function that sleeps, but does not use 100% CPU power (like Sleep())
    Last edited by samus250; 06-18-2008 at 09:08 PM.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by _Nate_ View Post
    So what would be the best way going about getting millisecond precision?
    Just do something like this:
    Code:
    	ticks = clock(); // gets the current ticks count
    	semiprimes = semiprimegen(parameters); // this function executes
    	ticks = clock() - ticks; // get the difference of ticks (results in the time the function ran)
    
    	// now print the time in seconds with two decimal points
    	printf("Took &#37;.2f seconds to generate semiprimes.\n", ticks / (double)CLOCKS_PER_SEC);
    Hope it helps.

  4. #19
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Err, I don't think you can actually make your own function that sleeps. Not in simple way. You would need to tell the OS to suspend your program. So you would still need a function for that.

    Why make your own function for that? I can only think of two things:
    1) Don't what to sleep the program for X seconds, but inform when you want it to sleep and wake-up. In this case there should be other functions like sleep_wait_for_signal.

    2) You want it to work with less CPU-power. In this case you are looking for something else. There was a thread about that.

    Why do you want to make your own sleep() if I may ask?

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    nahh, I was just curious.

    Thanks!

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sleep() as a system call will work together with the scheduler and the timing code in the system. Essentially, it will put the thread on a list of "sleeping until X ticks from now", which is sorted by the number of ticks until it's to wake up. In a simplified description, when each timer tick occurs, the OS will then deduct one from each entry in the list. If the value is zero, then it's put back on the "runnable" list, and will get run whenever the scheduler gets around to it.

    The same principle is used for WaitForXXXXObject where there is a timeout value involved. So if you want to do a Sleep of your own (that doesn't use CPU time), then you could use WaitForSingleObject() with a timeout to match the Sleep time, and give a event to wait for that never gets signalled. But that's in no way better than the Sleep function itself - just different.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The same principle is used for WaitForXXXXObject where there is a timeout value involved. So if you want to do a Sleep of your own (that doesn't use CPU time), then you could use WaitForSingleObject() with a timeout to match the Sleep time, and give a event to wait for that never gets signalled. But that's in no way better than the Sleep function itself - just different.
    In some way - it is better, WaitForSingleObject has precision of 1 ms while Sleep - about 10ms

    Instead of event - handle of the current process or thread could be used - they are not signaled till the process (thread) is finished - so no need to create event...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    In some way - it is better, WaitForSingleObject has precision of 1 ms while Sleep - about 10ms

    Instead of event - handle of the current process or thread could be used - they are not signaled till the process (thread) is finished - so no need to create event...
    Good points.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 05:15 PM
  3. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  4. world clock
    By nevermind in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 07:45 AM
  5. ANN: The Fourth Contest: Alarm Clock, sign up here
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 59
    Last Post: 08-10-2002, 12:24 AM

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