C++ 有哪些优化技巧可以让我的代码运行得更快

C++ 有哪些优化技巧可以让我的代码运行得更快,c++,optimization,random,stdvector,chrono,C++,Optimization,Random,Stdvector,Chrono,我正在走出我的对抗区,尝试制作一个随机数分布程序,同时也确保它仍然是一致的。 这是我的密码 这是randomdribution.h文件 #pragma once #include <vector> #include <random> #include <iostream> static float randy(float low, float high) { static std::random_device rd; static s

我正在走出我的对抗区,尝试制作一个随机数分布程序,同时也确保它仍然是一致的。 这是我的密码

这是randomdribution.h文件

#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;
};
#pragma一次
#包括
#包括
#包括
静态浮动随机数(低浮动、高浮动){
静态std::随机_设备rd;
静态std::mt19937随机(rd());
标准::均匀真实分布运行(低、高);
返回运行(随机);
}
typedef std::向量Vfloat;
类随机分布
{
公众:
随机分布();
随机分布(浮动百分比、浮动内容、整数容器);
~random分布();
void setvariables(浮动百分比、浮动内容、int容器);
Vfloat RunDistribution();
私人:
浮动分配器;
浮动百分比;
int jar_极限;
浮动内容;
浮点数分布;
Vfloat罐;
bool为0;
};
这是我的RandomDistribution.cpp

#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;
}
#包括“randomdribution.h”
随机分布::随机分布(){
}
RandomDistribution::RandomDistribution(浮动百分比、浮动内容、int容器):\u内容(内容)、jar\u限制(容器)
{
调整容器大小;
如果(百分比<0)
_百分比=0;
否则{
_百分比=百分比;
}
除法器=震击器极限*百分比;
is0=假;
}
随机分布::~randomdribution()
{
}
void RandomDistribution::setvariables(浮点百分比、浮点内容、int容器){
if(容器限制!=容器)
调整大小(容器);
_内容=内容;
jar_limit=容器;
is0=假;
如果(百分比<0)
_百分比=0;
否则{
_百分比=百分比;
}
除法器=震击器极限*百分比;
}
Vfloat RandomDistribution::RunDistribution(){
for(int i=0;i=jar|u limit|u contents<2){
Jar[i]=\u内容;
_内容-=罐[i];
is0=真;
}

如果(!\u%)Cheinan Marks在他的cppcon 2016演讲中有一些与随机生成器和朋友相关的基准测试和性能提示,他会提到一些快速生成器以及IIRC。我会从这里开始。

如果你在代码审查中发布这篇文章,欢迎你这样做,请确保使用一个描述代码功能的标题,例如“随机数分布程序”。您正在使用优化/发布模式进行编译,对吗?为什么不使用和?。同样的道理。您是在启用优化的情况下构建应用程序的吗?另外,请解释您在这里尝试执行的操作:
if(!\u%)
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";

    }
}