- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个 C++ 程序,用于通过“将随机点放入四分之一圆中并计算它们等”来计算圆周率。现在我的程序在我看来有点慢,我想过一些改进来加快它的速度(源代码在下面)。
我的第一个想法是使用 OpenMP 使其成为多线程的,即将 (I) 和 (II) 之间的代码拆分为多个线程,这样我就有了近十倍的轮数,而不必等待更长的时间(在八核系统上)。
我的另一个想法是使用全局变量和指针,这样我只需要复制指针而不是整数元组。缺点是 (idk)?
那么,我还能做些什么来加快程序的速度呢?我主要使用 Windows,但我也可以使用 Unix/Linux。
非常感谢!
代码部分:
#include <cstdlib>
#include <iostream>
#include <tuple>
#include <math.h>
#include <time.h>
#include <omp.h>
#include <sys/time.h>
#define RAND_MAX 32000
#define LOOPS 1000000
inline std::tuple<int, int> Throw_points(void)
{
int i = 0, j = 0;
i = rand() % 1000;
j = rand() % 1000;
return std::make_tuple(i, j);
}
inline bool is_in_circle(std::tuple<int, int> point)
{
if ((pow(std::get<0>(point), 2) + pow(std::get<1>(point), 2)) <= pow(1000, 2))
return true;
else
return false;
}
inline double pi(void)
{
srand(time(NULL));
long long int in_circle = 0;
long long int out_circle = 0;
for (int i = 0; i < LOOPS; i++)
{
if (is_in_circle(Throw_points()))
in_circle++;
out_circle++;
}
return double(in_circle) / double(out_circle) * 4;
}
通过pi()
调用
最佳答案
我只是玩了一下。实际上,对原始帖子(包括我自己的)的评论中的所有建议几乎没有任何区别。
但是,摆脱元组
inline void Throw_points(int&i, int&j)
{
i = rand() % 1000;
j = rand() % 1000;
}
inline bool is_in_circle(int i, int j)
{
return i*i + j*j < 1000000;
}
将程序加速了 5 倍。
我使用了这里的 boost::progress_timer 解决方案,顺便说一句:How to get the time elapsed running a function in C++
关于c++ - 加速 Monte Carlo Pi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19912701/
我最近一直在研究强化学习。为此,我一直在阅读萨顿的名著,但还有一些我还没有完全理解的地方。 对于蒙特卡洛学习,我们可以在首次访问和每次访问算法之间进行选择,并且可以证明两者都渐进地收敛到正确的解决方案
我编写了一个 C++ 程序,用于通过“将随机点放入四分之一圆中并计算它们等”来计算圆周率。现在我的程序在我看来有点慢,我想过一些改进来加快它的速度(源代码在下面)。 我的第一个想法是使用 OpenMP
我目前正在开发一个小程序,我想根据 cultureinfo 清楚地显示日期和时间,如下所示:2017 年 3 月 3 日星期日上午 7:46:57 但是,我希望可以选择任何国家,并根据他们写日期的方式
基本上,这个问题模拟了以下内容: 有一个装有 50 个绿球和 50 个红球的瓮。 我可以从 jar 里取出球,无需更换,规则如下:每取出一个红球,我将损失一美元,每取出一个绿色球,我将获得一美元。 我
更新: 我的编译命令是ghc -O2 Montecarlo.hs。我的随机版本是random-1.1,ghc版本是8.6.4,我的系统是macOS Big Sur 11.1(Intel芯片)。我用来测
我正在尝试使用组标识符 g 在 (y1,...,yN) 上对 ANOVA 进行排列检验.我应该使用 (1)/(g-1) (muhatj - muhat)^2 的总和作为测试统计量,而 muhatj 是
想象一下我递给你一个上面印有“-1”的乒乓球。然后我告诉你从标有“第一袋”的袋子中取出另一个乒乓球。这个袋子里有 30,000 个球,有的标有“-1”,有的标有“0”,有的标有“+1”。无论您抽到哪个
我正在尝试运行一个代码,通过使用蒙特卡罗积分对一维高斯分布方程进行积分来输出高斯分布。我正在尝试使用 mcint 模块。我定义了 mcint 模块中使用的高斯方程和采样器函数。我不确定 mcint 函
此练习的目的是创建营养摄入值的人口分布。之前的数据中有重复的度量,这些已被删除,因此每一行都是数据框中的唯一人。 我有这个代码,当使用少量我的数据框行进行测试时,它工作得很好。对于所有 7135 行,
我已经为 Hold'em Poker 编写了一个平衡器作为一个业余项目。它工作正常,但还有一件事我不满意:在整个模拟手的过程中,评估手的过程大约占用了35%的时间。与迭代和克隆大型数组等其他必须完成的
考虑从 [0,T) 开始按递增顺序给出的点 Y。我们要将这些点视为位于圆周 T 的圆上。现在考虑来自 [0,T) 的点 X 也位于圆周 T 的圆上。 我们说 X 和 Y 之间的距离是 X 中的每个点与
我目前正在使用 python 和 RPY 来使用 R 中的功能。 我如何使用 R 库生成蒙特卡罗样本,以尊重 2 个变量之间的相关性.. 例如 如果变量 A 和 B 具有 85% (0.85) 的相关
我正在尝试用 Python 实现一个简单的蒙特卡洛(我对此还很陌生)。来自 C 我可能走的是最错误的道路,因为我的代码对于我所要求的来说太慢了:对于 60 个 3d 粒子和周期性边界条件(PBC),我
我已经使用 SAS 很长时间了,现在我想用 R 翻译我的代码。我需要帮助来执行以下操作: 生成多个引导样本 对每个样本运行线性回归模型 通过复制样本将参数存储在新数据集中 为了更清晰,我编辑了这段代码
为了近似 Pi 的值,请考虑使用随机值填充数组并测试是否包含单位圆的随机方法, import random as rd import numpy as np def r(_): return rd.r
在我发现的所有计算 pi 的蒙特卡洛示例代码中,x 和 y 都是在 0 和 1 之间随机生成的。例如,示例代码如下所示 Ran rdm(time(NULL)); double x, y;
从这个数据集中,我有我的聚类分析分配的所有患者样本(总共 69 行),并且聚类被标记为第 3 列“Cluster.assigned”,总共 8 个聚类,每个聚类大小不等。其他列包含变量,其中我想测试数
我正在尝试在 Pytorch 上使用 Mc Dropout 实现贝叶斯 CNN,主要思想是通过在测试时应用 dropout 并运行多次前向传递,您可以获得来自各种不同模型的预测。我需要获得不确定性,有
这是我想用 R 做的算法: 模拟来自 ARIMA 的 10 个时间序列数据集模型通arima.sim()功能 将系列拆分为可能的子系列 2s , 3s , 4s , 5s , 6s , 7s , 8s
下面的 MWE 显示了两种对相同 2D 核密度估计进行积分的方法,这些估计是为 this data 获得的。使用 stats.gaussian_kde()功能。 对所有 (x, y) 执行集成低于阈值
我是一名优秀的程序员,十分优秀!