Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

Processor overloaded

Thread view: 
erimidtt@gmail.com - 09 Jan 2007 08:47 GMT
Hei

I'm trying to create a clock from scratch. If I run this code, the
processor overloads.
I think I know why, but I'm not shure how to avoid this.I didn't want
to make this a drag to read, so I only put in the problem area. The
variables described here is iniciated earlier in the code.

while (b < 10000000) {
    if (nesteSek == seku) {
        b++;
    } else {
        if (hour < 10) {
            System.out.print("0" + hour + ":");
        } else {
            System.out.print(hour + ":");
        }
        if (minu < 10) {
            System.out.print("0" + minu + ":");
        } else {
            System.out.print(minu + ":");
        }
        if (seku < 10) {
            System.out.print("0" + seku + "\t");
        } else {
            System.out.print(seku + "\t");
        }
        if (nesteSek >= 50) {
            int nyttSek = nesteSek - 50;
            if (seku == nyttSek) {
                break;
            }
        } else {
            if (seku >= (nesteSek + 10)) {
                break;
            }
        }
        telle++;
        count++;
        nesteSek++;
        break;
    }

As far as I can see, the first part of the loop overloads the
processor.
If someone can please help me find a simpler code that does not require
that much processor, I'd appreciate it.

Yergal
RedGrittyBrick - 09 Jan 2007 18:58 GMT
> Hei
>
> I'm trying to create a clock from scratch.

Maybe you should introduce Thread.sleep(1000); in the main loop?

> If I run this code, the processor overloads.

Perhaps you mean it runs at 100% utilisation for a short while.

> while (b < 10000000) {
>     if (nesteSek == seku) {
>         b++;
>     } else {
               <snip>
>     }
>
> As far as I can see, the first part of the loop overloads the
> processor.

If, prior to the while, nesteSek == seku and b == 0, your code will do
nothing but increment b ten million times in a tight loop.

> If someone can please help me find a simpler code that does not require
> that much processor, I'd appreciate it.

A faster and less CPU intensive way to achieve the same result would be
   b = 10000000;

Maybe you can construct a SSCCE that reproduces the problem. E.g.

public class Foo {
    public static void main(String[] args) {

    int nesteSek = 0;
    int seku = 0;
    int b = 0;
    etc

    // your code here

    }
}

Also, by using System.out.printf to add leading zeros your code would be
made a *lot* simpler.
Lew - 09 Jan 2007 23:36 GMT
erimidtt@gmail.com wrote:
>> while (b < 10000000) {
>>     if (nesteSek == seku) {
>>         b++;
>>     } else {
>                <snip>
>>     }

> If, prior to the while, nesteSek == seku and b == 0, your code will do
> nothing but increment b ten million times in a tight loop.
[quoted text clipped - 4 lines]
> A faster and less CPU intensive way to achieve the same result would be
>    b = 10000000;

I guess the state of the art for JIT doesn't optimize this loop away, huh?

- Lew
Ian Wilson - 10 Jan 2007 10:24 GMT
> erimidtt@gmail.com wrote:
>
[quoted text clipped - 15 lines]
>
> I guess the state of the art for JIT doesn't optimize this loop away, huh?

Mine doesn't.

Timing ...
While: 32 ms
Assign: 0 ms

-------------------------------------------------------
public class CPULoop {
    public static void main(String[] args) {
        System.out.println("Timing ...");

        // Q: Does JIT optimise this away?
        long start = System.currentTimeMillis();
        int b = 0;
        while (b < 10000000) {
            b++;
        }
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("While: " + elapsed + " ms");

        // Q: Is this faster?
        start = System.currentTimeMillis();
        int c = 0;
        c = 10000000;
        elapsed = System.currentTimeMillis() - start;
        System.out.println("Assign: " + elapsed + " ms");
    }
}
-------------------------------------------------------

Of course, 32ms is not really worth worrying about.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.