为什么两个线程的计算速度慢于一个?

时间:2015-06-28 22:45:02

标签: java multithreading cpu-cores

我正在研究用Java制作的基准测试应用程序。我在主线程上运行此代码两次,然后在两个单独的线程中运行一次:

if (testing == null) {
    testing = new byte[TEST_SIZE][TEST_SIZE][TEST_SIZE];
}

for (int x = 0; x < TEST_SIZE; x ++) {
    for (int y = 0; y < TEST_SIZE; y ++) {
        for (int z = 0; z < TEST_SIZE; z ++) {
            testing[x][y][z] = (byte)RANDOM.nextInt(100);
        }
    }
}

if (finished == Test.LOOP_COUNT - 1) {
    testing = null;
}

主线程上的任务比两个线程更快地完成,如应用程序的输出所示:

Starting test Array Handling with a single core.
Loop #1 finished in 1.820588011 seconds.
Loop #2 finished in 1.779667175 seconds.
Finished in 3 seconds.
Starting test Array Handling with multiple cores.
Loop #2 finished in 9.433253526 seconds.
Loop #1 finished in 9.465652985 seconds.
Finished in 9 seconds.

我在某处读到了一些内容,其中表示两个运行非常快的操作的线程不会像单个线程一样运行,但是处理要求更高的操作的两个线程的性能优于单个线程。我不认为是这种情况,因为每个循环都要求很高。我能想到的唯一原因是线程实际上并没有在自己的内核上运行。这可能是问题吗?我有一个2核4线程Intel Core i7-3537U。

修改

测试类:

package net.jibini.park.tests;

import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * @author zgoethel12
 */
public abstract class Test {

public static final Random RANDOM = new Random();
public static final int LOOP_COUNT = 2;

public static final CopyOnWriteArrayList<Test> tests = new CopyOnWriteArrayList<Test>();

public int finished = 0;
public int longestTime = 0;
public double timeSum = 0;

static {
    RANDOM.setSeed(481923);
    tests.add(new TestArray());
}

public abstract String getName();

public void runTest(final boolean multithread) {

    new Thread(new Runnable() {
            @Override
            public void run() {
                finished = 0;
                longestTime = 0;
                timeSum = 0;

                if (multithread) {
                    for (int i = 0; i < LOOP_COUNT; i ++) {
                        final int f = i;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                doLoop(f + 1);
                                Thread.currentThread().interrupt();
                            }
                        }).start();
                    }
                } else {
                    for (int i = 0; i < LOOP_COUNT; i ++) {
                        doLoop(i + 1);
                    }
                }

                while (finished < LOOP_COUNT) {
                    System.out.print("");
                }

                System.out.println("Finished in " + (multithread ? longestTime : (int)timeSum) + " seconds.");
                Thread.currentThread().interrupt();
            }
    }).start();

}

public void doLoop(int id) {

    long start = System.nanoTime();
    doTest(id);
    handleLoopFinish(id, start);

}

public abstract void doTest(int id);

public void handleLoopFinish(int id, long start) {

    long current = System.nanoTime();
    long difference = current - start;
    double seconds = (double)difference / 1000000000;
    if (seconds > longestTime) {
        longestTime = (int)seconds;
    }
    timeSum += seconds;
    System.out.println("Loop #" + id + " finished in " + seconds + " seconds.");
    finished ++;

}

}

阵列测试:

package net.jibini.park.tests;

/**
 *
 * @author zgoethel12
 */
public class TestArray extends Test {

public static final int TEST_SIZE = 512;

byte[][][] testing = null;

@Override
public void doTest(int id) {

    if (testing == null) {
        testing = new byte[TEST_SIZE][TEST_SIZE][TEST_SIZE];
    }

    for (int x = 0; x < TEST_SIZE; x ++) {
        for (int y = 0; y < TEST_SIZE; y ++) {
            for (int z = 0; z < TEST_SIZE; z ++) {
                testing[x][y][z] = (byte)RANDOM.nextInt(100);
            }
        }
    }

    if (finished == Test.LOOP_COUNT - 1) {
        testing = null;
    }

}

@Override
public String getName() {
    return "Array Handling";
}

}

2 个答案:

答案 0 :(得分:2)

您似乎只使用一个RANDOM对象。我担心这两个线程之间会共享,这可能会让它们变得非常慢。

尝试使用ThreadLocalRandom

答案 1 :(得分:0)

启动第二个线程,取决于您选择执行多线程的库,需要花费大量开销,而您似乎正在执行的操作(取决于TEST_SIZE的大小)可能非常有效地执行CPU,因为您以连续的方式遍历内存中的数组。

我已经在课程中指示内置的Thread Java库比ForkJoin框架有更多的开销,所以也许你会使用该库获得更多的预期结果。

作为一种资源,这是我在并发类中使用的一个不错的网站: http://homes.cs.washington.edu/~djg/teachingMaterials/spac/grossmanSPAC_forkJoinFramework.html

小心做热身跑步,如下所述。它们是确保您可以看到好处的关键。我们尝试做~100次跑步,热身约10次,以确保你得到一些好的平均值。由于上下文切换/其他计算机进程可能会增加试验的可变性,因此在多次运行中求平均有很大帮助!