- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在测试 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/
我有一个如下所示的数据框: import pandas as pd d = {'decil': ['1. decil','1. decil','2. decil','2. decil','3. dec
我有一些数据想要添加到我的应用中...大约 650 个类别(包括名称 + ID 号),每个类别平均有 85 个项目(每个都有一个名称/ID 号)。 iPhone会支持这么大的plist吗?我想首先在
我目前正在使用 Python 从头开始实现决策树算法。我在实现树的分支时遇到了麻烦。在当前的实现中,我没有使用深度参数。 发生的情况是,要么分支结束得太快(如果我使用标志来防止无限递归),要么如果
我在 Stack 上发现了这个问题 - Measuring the distance between two coordinates in PHP 这个答案在很多方面似乎对我来说都是完美的,但我遇到了
我目前正在清理一个具有 2 个索引和 2.5 亿个事件行以及大约同样多(或更多)的死行的表。我从我的客户端计算机(笔记本电脑)向我的服务器发出命令 VACCUM FULL ANALYZE。在过去的 3
这一切都有点模糊,因为该计划是相当深入的,但坚持我,因为我会尽量解释它。我编写了一个程序,它接受一个.csv文件,并将其转换为MySQL数据库的INSERT INTO语句。例如: ID Numbe
我有一个地址示例:0x003533,它是一个字符串,但要使用它,我需要它是一个 LONG,但我不知道该怎么做:有人有解决方案吗? s 字符串:“0x003533”到长 0x003533 ?? 最佳答案
请保持友善 - 这是我的第一个问题。 =P 基本上作为一个暑期项目,我一直在研究 wikipedia page 上的数据结构列表。并尝试实现它们。上学期我参加了 C++ 类(class),发现它非常有
简单的问题。想知道长 IN 子句是否是一种代码味道?我真的不知道如何证明它。除了我认为的那样,我不知道为什么它会闻起来。 select name, code, capital, pop
我正在尝试基于 C# 中的种子生成一个数字。唯一的问题是种子太大而不能成为 int32。有什么方法可以像种子一样使用 long 吗? 是的,种子必须很长。 最佳答案 这是我移植的 Java.Util.
我一直想知道这个问题有一段时间了。在 CouchDB 中,我们有一些相当的日志 ID……例如: “000ab56cb24aef9b817ac98d55695c6a” 现在,如果我们正在搜索此项目并浏览
列的虚拟列 c和一个给定的值 x等于 1如果 c==x和 0 其他。通常,通过为列创建虚拟对象 c , 一排除一个值 x选择,因为最后一个虚拟列不添加任何信息 w.r.t.已经存在的虚拟列。 这是我如
使用 tarantool,为什么我要记录这些奇怪的消息: 2016-03-24 16:19:58.987 [5803] main/493623/http/XXX.XXX.XXX.XXX:57295 t
我显然是 GitHub 的新手,想确保在开始之前我做的事情是正确的。 我想创建一个新的存储库,它使用来自 2 个现有项目的复刻/克隆。现有项目不是我的。 假设我想使用的 repo 被称为来自开发人员“
我的应用程序名称长度为 17 个字符。当安装在设备上时,它看起来像应用程序...名称。有没有办法在多行上显示应用程序名称?请帮忙。 最佳答案 不,你不能。我认为 iPad 支持 15 个字符来完整显示
我必须编写一个程序来读取文件中的所有单词,并确定每个单词使用了多少次。我的任务是使用多线程来加快运行时间,但是单线程程序的运行速度比多线程程序快。我曾尝试研究此问题的解决方案,但很多解释只会让我更加困
假设我在给定的范围内有一个位置pos,这样: 0 = newRange*newRange : "Case not supported yet"; // Never happens in my code
我试图在 Java 中将 unix 时间四舍五入到该月的第一天,但没有成功。示例: 1314057600 (Tue, 23 Aug 2011 00:00:00 GMT) 至 1312156800
我们的项目有在 CVS 中从现有分支创建新分支的历史。几年后,这导致了每次发布时更改的文件上的这种情况: 新版本:1.145.4.11.2.20.2.6.2.20.2.1.2.11.2.3.2.4.4
我有以下数据框: DAYS7 <- c('Monday','Tuesday','Wednesday','Thursday','Friday', 'Saturday', 'Sunday') DAYS
我是一名优秀的程序员,十分优秀!