On 8/11/2010 11:06 PM, Thomas Hruska wrote:
> On 8/10/2010 5:29 PM, sihd0 wrote:
>> Hello, I am a new and upcoming programmer and I was wondering if anyone 
>> knows how to limit the speed at which a program will execute. I am planning 
>> to begin research into factoring and would like to only use about 10% of the 
>> available resources on my home computer. So can anyone help me out here?
>
> What exactly is the purpose of limiting the speed of execution?  If it
> is to learn how the program operates, I suggest using a debugger and
> setting a breakpoint at the start of the program.  Then you can step
> through line by line.
>
> The best GUI-based debuggers will let you hover over variable names and
> see their values visually at each point where the debugger pauses the
> running program.
>
> If, for some strange reason, you truly need to limit the speed of a
> program as it executes, there are several tools out there that can do
> the job without burning CPU cycles.  Besides, most modern processors are
> multithreaded/multicore so simply burning CPU cycles won't do anything
> to slow down a program.

Oh.  I see.  I was reading the question incorrectly.  You only want your 
program to use some specific percentage of CPU (i.e. be resource-friendly).

It depends heavily on the target OS.  You will need to gain access to 
the operating system performance metrics for your OS.  If you only want 
to consume no more than 10% of the CPU, then you need to query the 
performance APIs for your OS (e.g. CPU utilization).  Most modern OSes 
allow for checking utilization on a per-process basis.

Each OS also has methods to put the currently executing thread to sleep 
for a specified amount of time.  APIs for this are available typically 
at the millisecond level (or smaller).

-- 
Thomas Hruska
CubicleSoft President

Barebones CMS is a high-performance, open source content management 
system for web developers operating in a team environment.

An open source CubicleSoft initiative.
Your choice of a MIT or LGPL license.

http://barebonescms.com/

Reply via email to