私は対面ゾーン外に移動していて、乱数配布プログラムを作成しようとしています。 はここ私のコードをより速く動かすためのいくつかの最適化のトリックはありますか?
#include "RandomDistribution.h"
RandomDistribution::RandomDistribution() {
}
RandomDistribution::RandomDistribution(float percent, float contents, int containers):_contents(contents),jar_limit(containers)
{
Jar.resize(containers);
if (percent < 0)
_percent = 0;
else {
_percent = percent;
}
divider = jar_limit * percent;
is0 = false;
}
RandomDistribution::~RandomDistribution()
{
}
void RandomDistribution::setvariables(float percent, float contents, int container) {
if (jar_limit != container)
Jar.resize(container);
_contents = contents;
jar_limit = container;
is0 = false;
if (percent < 0)
_percent = 0;
else {
_percent = percent;
}
divider = jar_limit * percent;
}
Vfloat RandomDistribution::RunDistribution() {
for (int i = 0; i < jar_limit; i++) {
if (!is0) {
if (i + 1 >= jar_limit || _contents < 2) {
Jar[i] = _contents;
_contents -= Jar[i];
is0 = true;
}
if (!_percent <= 0) {//making sure it does not get the hole container at once
_maxdistribution = (_contents/(divider)) * (i + 1);
}
else {
_maxdistribution = _contents;
}
Jar[i] = randy(0, _maxdistribution);
if (Jar[i] < 1) {
Jar[i] = 0;
continue;
}
_contents -= Jar[i];
}
else {
Jar[0];
}
//mixing Jar so it is randomly spaced out instead all at the top
int swapper = randy(0, i);
float hold = Jar[i];
Jar[i] = Jar[swapper];
Jar[swapper] = hold;
}
return Jar;
}
ソースコードは、プリントアウトし何
int main(){
RandomDistribution distribution[100];
for (int i = 0; i < 100; i++) {
distribution[i] = {RandomDistribution(1.0f, 5000.0f, 2000) };
}
Vfloat k;
k.resize(200);
for (int i = 0; i < 10; i++) {
auto t3 = chrono::steady_clock::now();
for (int b = 0; b < 100; b++) {
k = distribution[b].RunDistribution();
distribution[b].setvariables(1.0f, 5000.0f, 2000);
}
auto t4 = chrono::steady_clock::now();
auto time_span = chrono::duration_cast<chrono::duration<double>>(t4 - t3);
cout << time_span.count() << " seconds\n";
}
}
は、通常、これはRandomDistribution.hが、これは私のRandomDistribution.cppある
#pragma once
#include <vector>
#include <random>
#include <iostream>
static float randy(float low, float high) {
static std::random_device rd;
static std::mt19937 random(rd());
std::uniform_real_distribution<float> ran(low, high);
return ran(random);
}
typedef std::vector<float> Vfloat;
class RandomDistribution
{
public:
RandomDistribution();
RandomDistribution(float percent, float contents, int container);
~RandomDistribution();
void setvariables(float percent, float contents, int container);
Vfloat RunDistribution();
private:
float divider;
float _percent;
int jar_limit;
float _contents;
float _maxdistribution;
Vfloat Jar;
bool is0;
};
を提出された私のコード
ですサイクルごとに1秒から2秒の間。可能であれば、これを10分の1に減らしたいと思っています。これは完了までのプロセスのほんの一歩に過ぎず、私は100回以上それをやりたいと思っています。私はこれをスピードアップするために何ができますか、私はちょうどここで不足している何かのトリックか何か。 ここでタイムスタンプのサンプル
4.71113秒
1.35444秒
1.45008秒
1.74961秒
2.59192秒
2.76171秒です
1.90149秒
2.2822秒
2.36768秒
2.61969秒
これを歓迎するCode Reviewに投稿する場合は、「乱数配布プログラム」など、コードの内容についてわかりやすいタイトルを使用してください。 – Phrancis
最適化/リリースモードでコンパイルしていますよね? – Justin
なぜ[std :: uniform_int_distribution](http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution)と[friends](http://en.cppreference.com/w/cpp/concept/RandomNumberDistribution)? –