【问题标题】:what are some optimization tricks to make my code run faster有哪些优化技巧可以让我的代码运行得更快
【发布时间】:2017-11-19 01:39:17
【问题描述】:

我正在走出我的对抗区并尝试制定一个随机数分配程序,同时还要确保它仍然有点均匀。 这是我的代码

这是 RandomDistribution.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;
};

这是我的 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;
}

源代码

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";

    }
}

每个循环打印出来的时间通常在 1 到 2 秒之间。如果可能的话,我想把它降低到十分之一秒,因为这只是完成过程的一个步骤,我想运行它超过 100 次。我能做些什么来加快速度,任何技巧或我在这里缺少的东西。 这是时间戳的示例

4.71113 秒

1.35444 秒

1.45008 秒

1.74961 秒

2.59192 秒

2.76171 秒

1.90149 秒

2.2822 秒

2.36768 秒

2.61969 秒

【问题讨论】:

  • 如果您在 Code Review 上发布,欢迎您这样做,请确保使用描述性标题来说明代码的作用,例如“随机数分配程序”。
  • 您正在使用优化/发布模式进行编译,对吧?
  • 为什么不直接使用std::uniform_int_distributionfriends
  • I am not getting the results you're getting。同样的事情if I use Visual C++。您是否在打开优化的情况下构建应用程序?
  • 另外,请在此处解释您要执行的操作:if (!_percent &lt;= 0)。这种比较究竟意味着什么?不管是什么,我相信有更连贯的方式来表达你想要做的事情。

标签: c++ optimization random stdvector chrono


【解决方案1】:

Cheinan Marks 在他的 cppcon 2016 演讲 I Just Wanted a Random Integer! 中提到了一些与随机生成器和朋友相关的基准测试和性能提示,他还提到了一些快速生成器以及 IIRC。我会从那里开始。

【讨论】:

    猜你喜欢
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 2020-08-03
    相关资源
    最近更新 更多