J
jonasforssell
Hello Experts.
This program does not give expected performance boost on my P4 with
HyperThreading under Linux
/****************************************************************************/
public class ThreadTester implements Runnable {
int id;
public ThreadTester(int tid) {
this.id = tid;
}
public void run() {
System.out.println("Starting thread: " + id);
long stime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++)
for (int j = 0; j < 20000; j++) {
double p = i * j;
p = Math.sqrt(p);
}
stime = System.currentTimeMillis() - stime;
System.out.println("Solution for thread " + id + " took " +
stime + " ms");
}
public static void main(String[] args) {
if (args.length != 1) throw new
IllegalArgumentException("\n\nSyntax is 'java ThreadTester x' where x
is number of threads \n");
int cpu = Integer.parseInt(args[0]);
Thread[] t = new Thread[cpu];
for (int i = 0; i < cpu; i++)
t = new Thread(new ThreadTester(i));
for (int i = 0; i < cpu; i++)
t.start();
try {
for (int i = 0; i < cpu; i++) {
t.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**************************************************************************/
And this is my output
pc58410@gustav Impact $ java ThreadTester 1
Starting thread: 0
Solution for thread 0 took 5381 ms
pc58410@gustav Impact $ java ThreadTester 1
Starting thread: 0
Solution for thread 0 took 5379 ms
pc58410@gustav Impact $ java ThreadTester 2
Starting thread: 1
Starting thread: 0
Solution for thread 1 took 11247 ms
Solution for thread 0 took 11321 ms
pc58410@gustav Impact $ java ThreadTester 2
Starting thread: 1
Starting thread: 0
Solution for thread 1 took 11241 ms
Solution for thread 0 took 11325 ms
With an effective core distribution, this should have similar values as
the first runs (< 6000 ms)
What have I done wrong? I thought the JVM would make a good
distribution automatically?
Many thanks
/Jonas Forssell, Gothenburg, Sweden
This program does not give expected performance boost on my P4 with
HyperThreading under Linux
/****************************************************************************/
public class ThreadTester implements Runnable {
int id;
public ThreadTester(int tid) {
this.id = tid;
}
public void run() {
System.out.println("Starting thread: " + id);
long stime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++)
for (int j = 0; j < 20000; j++) {
double p = i * j;
p = Math.sqrt(p);
}
stime = System.currentTimeMillis() - stime;
System.out.println("Solution for thread " + id + " took " +
stime + " ms");
}
public static void main(String[] args) {
if (args.length != 1) throw new
IllegalArgumentException("\n\nSyntax is 'java ThreadTester x' where x
is number of threads \n");
int cpu = Integer.parseInt(args[0]);
Thread[] t = new Thread[cpu];
for (int i = 0; i < cpu; i++)
t = new Thread(new ThreadTester(i));
for (int i = 0; i < cpu; i++)
t.start();
try {
for (int i = 0; i < cpu; i++) {
t.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**************************************************************************/
And this is my output
pc58410@gustav Impact $ java ThreadTester 1
Starting thread: 0
Solution for thread 0 took 5381 ms
pc58410@gustav Impact $ java ThreadTester 1
Starting thread: 0
Solution for thread 0 took 5379 ms
pc58410@gustav Impact $ java ThreadTester 2
Starting thread: 1
Starting thread: 0
Solution for thread 1 took 11247 ms
Solution for thread 0 took 11321 ms
pc58410@gustav Impact $ java ThreadTester 2
Starting thread: 1
Starting thread: 0
Solution for thread 1 took 11241 ms
Solution for thread 0 took 11325 ms
With an effective core distribution, this should have similar values as
the first runs (< 6000 ms)
What have I done wrong? I thought the JVM would make a good
distribution automatically?
Many thanks
/Jonas Forssell, Gothenburg, Sweden