- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
只是为了娱乐,我想我会用蒙特卡洛实现来模拟一个组合问题。我在 VBA 中做了一个实现,然后作为练习,我想我会尝试用 C++ 编写它(我是一个完全的新手)来检查速度差异等。除了我不知道高级编码技术/技巧之外,我曾天真地认为只要将模型忠实地转移到 C++ 并尽可能地使用镜像函数/循环/变量类型等等,除了微小的调整之外,C++ 的强大功能会立即提高我的速度,因为我正在运行大量的模拟人生嵌入式排序等。好吧,情况恰恰相反,所以 C++ 实现一定存在严重错误,根据参数,它最多只有一半的速度。他们都收敛到相同的答案,所以在数学上很高兴他们工作。
问题:
假设您有 N 天随机分配 k 个考试,例如每天 2 个考试时段 (AM/PM)。说 2 天是完整考试日的概率是多少?我想我有一个封闭的形式,我现在相信,所以无论如何都想用 MC 进行测试。
算法启发式:
很简单,假设我们有 18 天、6 场考试、每天 2 个时段,我们想知道我们有 2 个完整天的概率。
(i) 模拟6个制服U_i(ii) 使用根据已分配的名额调整的制服在剩余的名额中随机分配考试名额,从而为考试分配名额。例如,如果 Exam 4 在 34 槽空间中分配了槽 4,但已经占用了 3 和 5,那么在 36 槽空间中,Exam_4 将分配到槽 6(这将是 rebase 后的第一个空闲槽)。已经通过一些嵌入式排序实现了这一点(在 VB Bubblesort/quicksort 中差异可以忽略不计,到目前为止在 C++ 中仅使用 bubblesort)。(iii) 只需将时间槽转换为天数,然后计算达到目标的模拟人生数。
呸——那只是背景。这样做的精神并不是真正优化算法,只是为了帮助我理解我做错了什么,导致在 C++ 中“镜像”时速度变慢!!
代码!
// monte carlo
#include "stdafx.h"
#include"AllocateSlots.h"
#include<vector>
#include<string>
#include<iostream>
#include<cmath>
#include<ctime>
using namespace std;
int main()
{
int i, j, k, m;
int days, exams, slotsperday, filledslotsperday, targetfulldays, filleddays;
long sims, count, simctr;
cout << "Days?: ";cin >> days;
cout << "Exams?: ";cin >> exams;
cout << "Slots Per Day?: ";cin >> slotsperday;
cout << "Filled Slots?: ";cin >> filledslotsperday;
cout << "Target Full Days?: ";cin >> targetfulldays;
cout << "No. of sims?: ";cin >> sims;
system("PAUSE");
//timer
clock_t start;
start = clock();
double randomvariate;
//define intervals for remaining slots
vector <double> interval(exams);
int totalslots = (days * slotsperday);
for (k = 1; k <= exams; k++)
{
interval[k-1] = 1 / (static_cast <double> (totalslots - k + 1));
}
vector <int> slots(exams); //allocated slots
vector <int> previousslots(exams); //previously allocated slots
vector <int> slotdays(exams); //days on which slots fall
srand((int) time(0)); //generates seed on current system time
count = 0;
for (simctr = 1; simctr <= sims; simctr++)
{
vector<int> daycounts(days); //initialised at 0
for (i = 1; i <= exams;i++)
{
//rand() generates integers in [0.0,32767]
randomvariate = (static_cast <double> (rand()+1))/ (static_cast <double> (RAND_MAX+1));
j = 1;
while (j <= totalslots - i + 1)
{
if (randomvariate < j*interval[i - 1]) break;
j++;
}
slots[i - 1] = j;
}
for (i = 2; i <= exams;i++)
{
previousslots.resize(i - 1);
for (m = 1; m <= i - 1; m++)
{
previousslots[m - 1] = slots[m - 1];
}
BubbleSort(previousslots);
for (k = 1; k <= i - 1;k++)
{
if (slots[i - 1] >= previousslots[k - 1])
{
slots[i - 1]++ ;
}
}
}
//convert slots into days
for (i = 1; i <= exams;i++)
{
slotdays[i - 1] = SlottoDays(slots[i - 1], slotsperday);
}
//calculate the filled days
filleddays = 0;
for (j = 1; j <= days; j++)
{
for (k = 1; k <= exams; k++)
{
if (slotdays[k - 1] == j)
{
daycounts[j - 1]++;
}
}
if (daycounts[j - 1] == filledslotsperday)
{
filleddays++;
}
}
//check if target is hit
if (filleddays == targetfulldays)
{
count++;
}
}
cout << count << endl;
cout << "Time: " << (clock() - start) / (double)(CLOCKS_PER_SEC) << " s" << endl;
//cout << (static_cast<double>(count)) / (static_cast<double>(sims));
system("PAUSE");
return 0;
}
以及 2 个辅助函数:
#include "stdafx.h"
#include"AllocateSlots.h"
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
//returns day for a given slot
int SlottoDays(int &examslot, int &slotsperday)
{
return((examslot % slotsperday == 0) ? examslot/ slotsperday: examslot/ slotsperday + 1);
}
//BubbleSort Algorithm
vector <int> BubbleSort(vector <int> &values)
{
int i;
int j;
int tmpSort;
int N = values.size();
for (i = 0; i < N;i++)
{
for (j = i + 1; j < N; j++)
{
if (values[i] > values[j])
{
tmpSort = values[j];
values[j] = values[i];
values[i] = tmpSort;
}
}
}
return values;
}
就是这样 - 就像我说的算法是 C++ 和 VBA 通用的,很高兴发布 VBA,但首先只是想知道上面是否有明显的东西。几乎是第一次这样做,使用 vector 等,独立的,自学的,所以肯定搞砸了一些事情,尽管已经设法让它运行了一些奇迹!非常感谢一些智慧的话 - 尝试通过这样的练习自学 C++,但我真正想要的是速度(当然还有数学准确性!)用于更大的项目。
仅供引用,在我的 18 天、6 次考试、每天 2 个空位、2 天的填充时间中,它应该收敛到大约 3.77%,这是在 VBA 中的 38 秒和 145 秒的 1mm 模拟机上实现的x64 windows7 上的 duocore 2.7G i7 4GB RAM 笔记本电脑。
最佳答案
从评论中的讨论来看,您可能正在以 Debug
模式运行您的程序。这会关闭一些优化,甚至会生成一些额外的代码。
要在 Release 模式下运行,请在标准工具栏中查找 Solution Configurations
下拉菜单,然后使用下拉菜单将 Debug
更改为 Release
。
然后重建您的解决方案并重新运行您的测试。
要在 Visual Studio 中进一步探索程序性能,您需要使用性能探查器工具。 Microsoft 文档站点上有一个关于使用 Performance Profiler 的教程(包括视频):Profile application performance in Visual Studio .还有 Quickstart: First look at profiling tools还有一大堆:都在Profiling in Visual Studio下部分。
关于c++ - C++ 中的组合练习看到速度与 VBA 相比有所下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50820159/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!