The fastest way to create random vectors for benchmarking

So, I just play around implementing some sorting algorithms in C ++, but I find it annoying to compare them at the moment because of how long it takes to not run the algorithm, but to create the input. I am currently testing each input length (1000, 2000, ...) 10 times to get some average time. For each of these 10 times, I create a new random of the vectorcorrect length by doing:

    // Each of the 10 times.
    for(int j = 0; j < 10; j++) {

        A.clear();

        // 'i' is the current input size.
        for(int k = 0; k < i; k++) {
            A.push_back(rand() % 10000);
        }

        // Other stuff
    }

Is there a better way to do this? Do I have to worry about closing rand () at 10,000, or is it just my brain OCD like round numbers? (That is, whether this modulo operation can really take a significant amount of time when you consider that it has been completed up to - at the moment - 10,000 for each out of 10 cycle). Alternatively, should I really create a new vector every time I run Sort? I did this because I felt that it was possible that the created vector could be biased, and therefore, if it was generated and then used 10 times, the answer could be completely disabled ...

+3
source share
3 answers

Is there a better way to do this?

, , , . , std::vector, . , pre incrementing (++ var var ++) . , , , . , , , , unsigned short for.

, . , . , , , .

, , , , , . , , .

    A.reserve(i * i);
    for(unsigned short j = 0; j < 10; ++j) {            
        for(unsigned short k = 0; k < i; ++k) 
            A[k + (i*10)] = rand();                
        // Other stuff
    }

Edit

: 10 , unsigned char, . Win32, , .

    A.reserve(i * i);
    for(unsigned char j = 0; j < 10; ++j) {            
        for(unsigned char k = 0; k < i; ++k) 
            A[k + (i*10)] = rand();                
        // Other stuff
    }
+1

cplusplus.com(http://www.cplusplus.com/reference/stl/vector/), :

" , , . , , , - vector::reserve."

vector::reserve .

EDIT: random_shuffle (http://www.cplusplus.com/reference/algorithm/random_shuffle/), (, random_shuffle ).

+1

, :

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sstream>

int main(int argc, char* argv[]){
    if (argc < 2){
        printf("No arguments found\n");
        exit(1);
    }
    int maxi;
    maxi = atoi(argv[1]);
    int * a;
    a = new int [5];

    std::stringstream ss;
    ss << maxi;
    printf(ss.str());
    printf("\n");
}
0

Source: https://habr.com/ru/post/1756341/


All Articles