Looking for a way to increase CPU usage

Discussion in 'Perl Misc' started by A. Lewenberg, Feb 21, 2004.

  1. A. Lewenberg

    A. Lewenberg Guest

    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.
     
    A. Lewenberg, Feb 21, 2004
    #1
    1. Advertising

  2. A. Lewenberg

    Roland Mösl Guest

    > 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
     
    Roland Mösl, Feb 21, 2004
    #2
    1. Advertising

  3. 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.
     
    James Willmore, Feb 21, 2004
    #3
  4. On 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.


    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/
     
    David Efflandt, Feb 21, 2004
    #4
  5. A. Lewenberg

    Miser Guest

    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.
     
    Miser, Feb 21, 2004
    #5
  6. A. Lewenberg

    Anno Siegel Guest

    Miser <> 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
     
    Anno Siegel, Feb 21, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vijay Ram

    How to fetch CPU Usage from DOT Net

    Vijay Ram, Sep 8, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    5,166
    Fergus Cooney
    Sep 22, 2003
  2. Steve Brecher

    Increase WinXP/jre CPU usage?

    Steve Brecher, Nov 13, 2006, in forum: Java
    Replies:
    32
    Views:
    1,778
    Patrick May
    Nov 23, 2006
  3. hvt
    Replies:
    0
    Views:
    1,215
  4. hvt
    Replies:
    0
    Views:
    1,477
  5. pavunkumar

    How , system cpu and user cpu times calculates

    pavunkumar, Feb 27, 2009, in forum: C Programming
    Replies:
    1
    Views:
    360
Loading...

Share This Page