1. If the whole code uses 100&#37; 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. Originally Posted by C_ntua
If the whole code uses 100&#37; 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())

3. Originally Posted by _Nate_
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. 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. nahh, I was just curious.

Thanks!

6. 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

7. 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...

8. Originally Posted by vart
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