aspose file tools*
The moose likes Performance and the fly likes Two thread slow on dual core? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Two thread slow on dual core?" Watch "Two thread slow on dual core?" New topic
Author

Two thread slow on dual core?

Saverio Miroddi
Greenhorn

Joined: Jun 16, 2006
Posts: 21
I have a simple class which converts data in two different ways and checks if the output is the same.

The strange thing is I have a dual core(Centrino 1.66), and if I launch a thread only:

new Check((byte)-128, (byte)127).start();

it takes about 800 ms for every cycle, with a CPU occupation of about 50-60% total.

so i thought to launch two threads splitting the work:
new Check((byte)-128, (byte)0).start();
new Check((byte)-128, (byte)0).start();

now it takes about 600 ms for every cycle of every thread, and a CPU occupation of about 70-80%.

so I have two questions:

- why occupation doesn't rise up to 100%?
- why the JVM doesn't manage the two thread correctly, even if they're totally separated?

assertion are disable (i don't care about routine correctness).
i post the source code (very simple) down.

bye!
saverio


class Check extends Thread {
private byte start, end;
private long lastTime;

public Check(byte start, byte end) {
this.start = start;
this.end = end;
}

public void run() {
lastTime = System.currentTimeMillis();
for (byte b1 = start; b1 < end; b1++) {
long now = System.currentTimeMillis();
out.println(now - lastTime);
lastTime = now;

for (byte b2 = (byte)-128; b2 < (byte)127; b2++) {
for (byte b3 = (byte)-128; b3 < (byte)127; b3++) {
for (byte b4 = (byte)-128; b4 < (byte)127; b4++) {
byte[] byteArr = new byte[] {
b1, b2, b3, b4
};
assert(vax2long(byteArr) == vax_to_long(byteArr)):
vax2long(byteArr) + "/" + vax_to_long(byteArr);
}
}
}
}
}

public final static long vax2long(byte[] b) {
long lngTmp =
(0x0FF & b[0]) |
(0x0FF & b[1]) << 8 |
(0x0FF & b[2]) << 16 |
(0x0FF & b[3]) << 24;

return lngTmp;
}

public final static long vax_to_long(byte[] b) {
long lngTmp =
(long) (0x0ffL & b[0]) |
((0x0ffL & (long) b[1]) << 8) |
((0x0ffL & (long) b[2]) << 16) |
((0x0ffL & (long) b[3]) << 24);

return lngTmp;

}
}


Using a text editor for programming.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
why occupation doesn't rise up to 100%?
- why the JVM doesn't manage the two thread correctly, even if they're totally separated?


1. Probably the operating system has other things to do
2. What did you expect it to do?

Bill
Saverio Miroddi
Greenhorn

Joined: Jun 16, 2006
Posts: 21
I was wrong about timings (two threads go faster), however CPU occupation doesn't reach full load even if there aren't any other processes working.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
even if there aren't any other processes working.


That is not necessarily true - operating sytems do many things without consulting the user. My Windows systems occasionally start doing lots of disc operations for no apparent reason for example. The task manager shows loads of mysterious processes just hanging around.

Bill
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 420
    
    2

Normally to max out CPU's you go into tight loops and do lots of math ...


Check your OS e.g. does your kernel support your chip ...

Check other Apps....

Check shared resources between threads ...

i.e. check calls to static members , remove them all and re time

i.e. the first thing that occurred is what is 'out' wired up to, note your outputting in a loop, any form of I/O particularly shared I/O will drop the CPU utilisation depending on how its implemented i.e. if you only have one file log / VDU think about how the CPU's OS copes with that or not. The OS'es must be damm efficient, I/O buffers whatever as most multi threaded apps seem to pour logs down the same hole, personally I always time stamped my output and put them in per thread logs, as usually I only wanted to see ones thread output at a time and could always combine them back up if needed. I know some logs are ultra fast but I'ed love to see some stats in a multi threaded environment if some one has some ??


Would love to run some test figures but don't have the (multi core)hardware :-(


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Saverio Miroddi
Greenhorn

Joined: Jun 16, 2006
Posts: 21
thank you!
however, if you pass me some class, i can do tests for you :-)

saverio
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Because the OS is not going to yield complete control of the CPU to your program. So sometimes your processes/threads will be suspended for other processes/threads. These other processes/threads to not have to tax the CPU 100%. They can in fact, sit idle, but they still will get some of the CPU time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two thread slow on dual core?