> 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.