vendredi 8 mai 2015

Overhead of threads on performance

I'm trying to increase the performance of an app by adding threads to do concurrent tasks. The results I've gotten are very confusing to me and make me think there is some kind of thread related overhead of which I am not aware. Below are two copies of the same code with the exception that one uses threads and the other doesn't. The one that doesn't use threads runs four times faster than the one that uses threads. I'm testing using my device which is a Samsung note 4 with a quad processor. Any insights will be highly welcome.

Thanks,

cwm

  public void testThreads() throws InterruptedException {
  startMilli = System.currentTimeMillis();
  Thread t1 = new Thread() {
        public void run() {
            load1();
        }
    };

    Thread t2 = new Thread() {
        public void run() {
            load2();
        }
    };
    t1.start();
    t2.start();
    t1.join();
    t2.join();
  //  load1();
  //  load2();
    stopMilli = System.currentTimeMillis();
    diffMilli = stopMilli - startMilli;
    startMilli = System.currentTimeMillis();
}
public void load1() {
    List<Integer> list1 = new ArrayList<Integer>();
    for(i = 0; i<100000; i++) {
        list1.add(i);
    }
}

public void load2() {
    List<Integer> list2 = new ArrayList<Integer>();
    for(j = 100000; j<200000; j++){
        list2.add(j);
    }
}

  public void testThreads() throws InterruptedException {
  startMilli = System.currentTimeMillis();

    load1();
    load2();
    stopMilli = System.currentTimeMillis();
    diffMilli = stopMilli - startMilli;
    startMilli = System.currentTimeMillis();
}
public void load1() {
    List<Integer> list1 = new ArrayList<Integer>();
    for(i = 0; i<100000; i++) {
        list1.add(i);
    }
}

public void load2() {
    List<Integer> list2 = new ArrayList<Integer>();
    for(j = 100000; j<200000; j++){
        list2.add(j);
    }
}

Aucun commentaire:

Enregistrer un commentaire