gpt4 book ai didi

java - 为什么单线程操作比无线程操作花费的时间长3倍?

转载 作者:行者123 更新时间:2023-12-01 23:08:38 27 4
gpt4 key购买 nike

我正在测试 Java 中的并发性,我的目标是确定拥有多个线程是否确实有益,但是,我得到的结果并不一致。我正在尝试优化阶乘函数,对于这个特定的测试,我使用 1e9!我想要结果对 1e9+7 取模,这样它就不会溢出。首先,我根据 number_of_threads 划分数量,并将其工作分别分配给每个线程。然后我会正常进行并比较我得到的时间。似乎当 number_of_threads = 4 时,我得到的结果比没有线程的版本更好,这是有道理的,因为我的 CPU 有 4 个核心。正如预期的那样,任何数量大于 4 的线程都会比只有 4 个线程的时间更慢。但是,当使用少于 4 个线程执行此操作时,结果会变得很大,例如,使用 1 个线程,我希望它的持续时间与执行相同它没有线程+开销。没有线程时,我得到 6.2 秒,而使用 1 个线程时,我得到 19.3 秒,这差别太大了,不仅仅是开销。

为了测试原因,我在 run 方法上放置了一些计数器,有时,执行其中的 for 的一个周期似乎需要超过一毫秒,但它不应该,因为它只是两个操作加上计时器。

public class Calc implements Runnable{
long min, max, mod, res;
Res r;
public Calc(long min, long max, long mod, Res r) {
this.min = min;
this.max = max;
this.mod = mod;
res = 1;
this.r = r;
}
public void run() {
for(long i = min; i <= max; i++) {
res *= i;
res %= mod;
}
r.addup(res);
}
}

public class Res{
long result;
long mod;
public Res(long mod) {
result = 1;
this.mod = mod;
}
public synchronized void addup(long add) {
result *= add;
result %= mod;
}
public long getResult() {
return result;
}
}

public class Main{
public static void main(String args[]) {
long startTime = System.nanoTime();
final long factorial = 1000000000L;
final long modulo = 1000000007L;
Res res = new Res(modulo);
int number_of_threads = 1;
Thread[] c = new Thread[number_of_threads];
long min = 1, max = factorial/(long)number_of_threads;
long cant = max;
for(int i = 0; i < number_of_threads; i++) {
if((long)i < (factorial % number_of_threads))max++;
c[i] = new Thread(new Calc(min, max, modulo, res));
c[i].start();
min = max +1;
max += cant;
}
for(int i = 0; i < number_of_threads; i++) {
try {
c[i].join();
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(res.getResult());
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println((double)totalTime/1000000000L);
}
}

当 number_of_threads = 1 时,我需要 19.3 秒。当 number_of_threads = 2 时,我得到 10.1 秒。当 number_of_threads = 3 时,我得到 7.1 秒。当 number_of_threads = 4 时,我得到 5.4 秒。在没有线程的情况下执行此操作时,我得到 6.2 秒(我用相同的方法计算了这个时间)

只有 1 个线程和没有线程之间应该没有太大区别,对于 2 和 3 线程来说,它应该比没有线程更快。为什么会这样?有什么办法可以解决吗?谢谢。

编辑:添加无线程版本

public class Main{
public static void main(String args[]) {
long startTime = System.nanoTime();
final long factorial = 1000000000L;
final long modulo = 1000000007L;

long res = 1;
for(long i = 1; i <= factorial; i++) {
res *= i;
res %= modulo;
}
System.out.println(res);
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println((double)totalTime/1000000000L);
}

}

最佳答案

发生这种情况是因为您的“无线程”(仅主线程)版本使用了更容易优化的所有编译时常量。

如果您确保编译器不会提前知道这些值是什么:

class Main{
public static void main(String args[]) {
long factorial = Long.parseLong(args[0]);
long modulo = Long.parseLong(args[1]);
long startTime = System.nanoTime();

long res = 1;
for(long i = 1; i <= factorial; i++) {
res *= i;
res %= modulo;
}
System.out.println(res);
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println((double)totalTime/1000000000L);
}

}

那么它会明显变慢:

$ java Main 1000000000 1000000007
698611116
11.368487148

在我的机器上,甚至比您另一台机器的 1 线程情况稍微多一点:

$ java Main
698611116
10.709532315

类似地,如果您修改 1 线程示例以使用 .run() 而不是 .start(),所有内容都将在主线程上运行,但有1 线程情况下不会有任何加速。

关于java - 为什么单线程操作比无线程操作花费的时间长3倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385302/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com