samedi 18 novembre 2017

what are some optimization tricks to make my code run faster

i'm moving outside my confront zone and trying to make a random number distribution program while also making sure it is still somewhat uniform. here is my code

this is the RandomDistribution.h file

#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;
};

this is my 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;
}

source code

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

    }
}

what prints out is usually between 1 to 2 seconds for each cycle. i want to bring it down to a tenth of a second if possible cause this is gonna be only one step of the process to completion and i want to run it alot more then 100 times. what can i do to speed this up, any trick or something i'm just missing here. here is a sample of the time stamps

4.71113 seconds

1.35444 seconds

1.45008 seconds

1.74961 seconds

2.59192 seconds

2.76171 seconds

1.90149 seconds

2.2822 seconds

2.36768 seconds

2.61969 seconds




Aucun commentaire:

Enregistrer un commentaire