Performance Evaluation on Single Thread and Multi-Thread

27 Feb 2011 by

Threading could eventually improve system capacity as multi-core and multi-processor are more common in commodity computer. We can spend less than USD 1000 to get Core-i3, Core-i5 or even Core-i7. So threading technique for enabling-threading tasks could maximize the overall system performance. Be remember that not all tasks could take advantage of multi-threading. And multi-thread is not always good since its overhead and cost in coordinating data. I will discuss this scenario in the later post.

Objective

This article is to convince developers, system managers, researchers to think of thread for performance demanding applications such as Image processing, Data mining, Natural Language Processing, Parallel Database Query etc.

Methodology

We develop 2 Java programs to calculate Fibonacci of a value of 45. One program is Single Thread while another uses Multi Thread. Our Fibonacci algorithm is using recursive to stimulate power and memory consumption. In each program, we run 5 times in the same environment.

Experiment Environment

Parameter Value
Operating System Windows 7 Ultimate x64
JDK Version 1.6u21
CPU Intel Core 2 Duo P9500 @ 2.54GHz
Memory 4GB

Result

Java Single Thread vs. Multi-thread

The graph shows the result of performance comparison between single thread and multi-thread in the same experimental environment. The x-axis shows the number of run while duration (millisecond) expresses in the y-axis. From the graph we can see that at any run, multi-thread outperforms single thread. The performance almost gains double for multi-thread. Be notice that, this experiment has done on Core 2 Duo architecture.

Moreover, I would like to show CPU consumption while doing the experiment in the figure below. From the figure, I investigates that single thread utilizes the CPU almost only 50% of any run whereas multi-thread consumes 100% of CPU. It might be the reason that multi-thread outperforms single thread in this scenario.

CPU Consumption on Single Thread

CPU Consumption on Multi-Thread

 

Source Code

Below is the full source code in this experiment.

SingleThreadFibonacci.java

public class SingleThreadFibonacci {
  public static void main(String[] args){
    long startTime = System.currentTimeMillis();
    for(int i=0; i<5; i++){
      System.out.println("Fib=" + fibonacci(45));
    }
    long stopTime = System.currentTimeMillis();
    System.out.println("Duration=" + (stopTime - startTime));
  }

  private static long fibonacci(int n){
    if(n==0) return 0L;
    if(n==1) return 1L;

    return (fibonacci(n-1) + fibonacci(n-2));
  }
}

MultiThreadFibonacci.java

public class MultiThreadFibonacci extends Thread{
  public static void main(String[] args){
    Thread[] t = new Thread[5];

    long startTime = System.currentTimeMillis();
    for(int i=0; i<5; i++){
      t[i] = new MultiThreadFibonacci();
      t[i].start();
    }

    for(int i=0;i<5;i++){
      try{
        t[i].join();
      }catch(InterruptedException ie){}
    }
    long stopTime = System.currentTimeMillis();
    System.out.println("Duration=" + (stopTime-startTime));
  }

  @Override
  public void run(){
    System.out.println("Fib=" + fibonacci(45));
  }

  private long fibonacci(int n){
    if(n==0) return 0L;
    if(n==1) return 1L;

    return (fibonacci(n-1) + fibonacci(n-2));
  }
}

Conclusion

Better program on multi-thread can significant improve total system performance. However, for future investigation, I will look on Core i3, Core i5 and Core i7 architecture to better visualize the impact of multi-thread on multi-core CPU.

One response so far

Leave a Reply