[concurrency-interest] Multi-core testing, help with findings

David Harrigan dharrigan at gmail.com
Mon Dec 11 06:14:59 EST 2006


Hi All,

I've recently acquired a nice new shiny core 2 duo (2 x 2.0Ghz) laptop and I
thought I would try out a test of threading in it. So, I wrote a simple
class (see below). However, my findings are curious and I would like if
possible someone to explain why they are slower on my multi-core system than
my older system which was a Pentium-M @ 2.33Ghz. Both machines, apart from
the processor are near enough identical - same disk speed, same type of
memory (667Mhz DDR2 2GB) etc..

After 20 runs of my program on the core 2 duo, the average time was : 6975ms 
After 5 runs of my program on the Pentium-M, the average time was : 2735m

I suspect it's because with two processors they are both contending for main
memory. Notice that I have the counter as volatile which forces the variable
to flush out to memory each time - since this is what I'm interested in
testing - real world stuff where things are synch'ed (when it wasn't
volatile, the change was dramatic - because the core 2 duo has 4MB of cache
it was extremely fast, whereas the Pentium-M with only 1MB of cache was a
lot lot slower)...




import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class ThreadTest {

    private static final int howMany = 1000;
    private static volatile boolean finished;
    final CyclicBarrier barrier = new CyclicBarrier(howMany, new Runnable()
{
        public void run() {
            finished = true;
        }
    });

    public static void main(String[] args) {
        ThreadTest t = new ThreadTest();
        long total = 0;
        for(int i = 0 ; i < 20 ; i ++) {
            long elapsedTime = t.doIt();
            total += elapsedTime;
            System.out.println("Run #" + i + " : elapsed time = " +
elapsedTime + "ms");
        }
        System.out.println("Average time = " + (total / 20) + "ms");
    }

    private long doIt() {
        long startTime = System.currentTimeMillis();
        for(int i = 0; i < howMany; i++) {
            new Thread(new Worker()).start();
        }
        while(!finished);
        long endTime = System.currentTimeMillis();
        return (endTime - startTime);
        
    }
       
    class Worker implements Runnable {
        volatile int counter;
        public void run() {
            for(counter = 0 ; counter < 1000000 ; counter++);
            try {
                barrier.await();
            } catch(InterruptedException e) {
                return;
            } catch(BrokenBarrierException e) {
                return;
            }
        }
    }
}


-=david=-
-- 
View this message in context: http://www.nabble.com/Multi-core-testing%2C-help-with-findings-tf2793302.html#a7792970
Sent from the JSR166 Concurrency mailing list archive at Nabble.com.



More information about the Concurrency-interest mailing list