Looking for a way to increase CPU usage

A

A. Lewenberg

I am looking for a perl code fragment that, given a number n will take
roughly n seconds of CPU time to complete. Note well that I do not
care how much _real_ time it takes: I want it to take n seconds of CPU
time to finish. Even if the CPU time it takes is within 50% of n that
would be OK.

I realize I could just do some experiments with tight loops to find
some code that would do what I want, but that would probably only work
on my current machine. I would like the code to work on different
platforms and hardware configurations.

Has anyone done this sort of thing before?

Thanks.
 
R

Roland Mösl

I am looking for a perl code fragment that, given a number n will take
roughly n seconds of CPU time to complete. Note well that I do not
care how much _real_ time it takes: I want it to take n seconds of CPU
time to finish. Even if the CPU time it takes is within 50% of n that
would be OK.

This is a sensless waste of energy.

The CPU uses more power than for a simple wait statement.

Also it's noise terror for the user.

The fan of my notebook usual does not work.
A complete silent notebook.

Only after maybe half miunte of full CPU usage,
the fan starts to cool.

So is, what You want a very bad energy wasting
and noise creating programming style
 
J

James Willmore

I am looking for a perl code fragment that, given a number n will take
roughly n seconds of CPU time to complete. Note well that I do not
care how much _real_ time it takes: I want it to take n seconds of CPU
time to finish. Even if the CPU time it takes is within 50% of n that
would be OK.

Are you looking to limit the time the script runs total, or actual
processing time?

If the former, you could use 'alarm'. The alarm function will allow you
to limit the amount of time the script runs.

perldoc -f alarm for more info, plus perldoc perlipc

I, personally, am not sure if you can limit the amount of processor the
script can "chew" on through Perl. You can, with some shells, set limits
on what a user can (not) do (ulimit and limit come to mind). However,
it's not a 100% solution (because the user can alter the soft limits in
some cases).

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Fortune's Real-Life Courtroom Quote #52: Q: What is your name?
A: Ernestine McDowell. Q: And what is your marital status? A:
Fair.
 
D

David Efflandt

I am looking for a perl code fragment that, given a number n will take
roughly n seconds of CPU time to complete. Note well that I do not
care how much _real_ time it takes: I want it to take n seconds of CPU
time to finish. Even if the CPU time it takes is within 50% of n that
would be OK.

If all you want to do is pause, you would use: sleep $n;

Example if you want to do something during that time:

#!/usr/bin/perl -w
my $n = 3;
my $end = time -1 + $n;
$| = 1; # unbuffered output for test dots
print "start " . localtime();
while (1) {
last if (time > $end);
print "."; # or do something useful
}
print scalar localtime() . " done\n";
 
M

Miser

From some of the posts here it appears many have misinterpreted the
OP's intent. I believe he is looking for a CPU stressor, something to
cause increased CPU usage as an artificial load for a specified time
period.
 
A

Anno Siegel

Miser said:
From some of the posts here it appears many have misinterpreted the
OP's intent. I believe he is looking for a CPU stressor, something to
cause increased CPU usage as an artificial load for a specified time
period.

That's what I thought, and since it's fun to write a busy wait,
here goes:

sub consume {
my $limit = cputime() + shift;
1 while cputime() < $limit;
}

# return whatever defines cpu time in your context
sub cputime {
my ( $user, $system) = times;
$user + $system;
}

This should consume almost exactly the requested amount of time, plus
a tiny overshoot.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top