Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Looking for a way to increase CPU usage

Reply
Thread Tools

Looking for a way to increase CPU usage

 
 
A. Lewenberg
Guest
Posts: n/a
 
      02-21-2004
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.
 
Reply With Quote
 
 
 
 
Roland Mösl
Guest
Posts: n/a
 
      02-21-2004
> 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


--
Roland Mösl
http://www.pege.org Clear targets for a confused civilization
http://web-design-suite.com Web Design starts at the search engine

 
Reply With Quote
 
 
 
 
James Willmore
Guest
Posts: n/a
 
      02-21-2004
On Sat, 21 Feb 2004 09:28:51 -0800, A. Lewenberg wrote:

> 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.
 
Reply With Quote
 
David Efflandt
Guest
Posts: n/a
 
      02-21-2004
On 21 Feb 2004 09:28:51 -0800, A. Lewenberg <(E-Mail Removed)> wrote:
> 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";

--
David Efflandt - All spam ignored http://www.de-srv.com/
 
Reply With Quote
 
Miser
Guest
Posts: n/a
 
      02-21-2004
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.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-21-2004
Miser <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
increase in cpu usage on locking and locking the system sowmya.rangineni@gmail.com Computer Support 0 06-15-2007 12:06 PM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:07 PM
Increase WinXP/jre CPU usage? Steve Brecher Java 32 11-23-2006 09:37 AM
Increase CPU cycles and RAM usage google@joriz.is-a-geek.net Computer Information 3 09-16-2005 10:04 PM
Increase CPU cycles and RAM usage google@joriz.is-a-geek.net Computer Information 1 09-14-2005 02:18 AM



Advertisments