Hi this is my first post. I am writing a program that generates random numbers and puts them in a container(array right now) then compares them to another container of the same type. Keeps generating a predetermined number of random numbers until the 2 containers match(like 7 numbers in each container).
I was wondering what the fastest set up would be. I tried using pointers to the 2 arrays instead of indexing and it was 1 second slower at generating 100 million sets of numbers.(ie (4,5,3,7,7,10,4)) Prlby because after generating the containers numbers and inserting them it has to reinitialize it begin(); To start at the beging of the array. Likewiseto the Pointer to the other array.
Also its generating and inserting linearally, as with the comparison loop. So random access is not important. Any help would be much appreciated
Mike
not static at the moment, ill try this memcmp() out. The first array is user entered digits. Should i make it static? Havent worked with static for a while... As for not putting the random in a container. I did that for the earlier version, but i want the whole container of numbers generated before comparison starts. Thankyou for the quick reply by the way.
You want to choose 8 specific numbers through random generation. That is a very slim chance (and its use is puzzling me :) Still, you can check if the first number matches after it is randomly generated, then the second, etc., instead of generating all 8 and then performing comparison. In most cases, even the first couple of numbers will not match, so you can either generate the rest without storing and comparing them (if you want to simulate changing the state of the random generator), or you can entirely skip the remaining numbers.
Thanks simeonz, i love the chances :) started out with the 1 number at a time method, but liked the whole array being there and initialized. All the numbers in each array go together. Only generating the first couple, although much faster, i dont like. Im wondering if using a different container would improve the speed of this loop.
Built-in arrays are the fastest containers: since they are built-in they don't have any overhead, all operations on them are just integer arithmetic handled directly by the processor
Random access is the fastest way to access data ( it has constant complexity )
Thanks Bazzy i was concered beacuse somone said that beacuse it uses an index and it has to multiply the index by the type inside it it was slower then others. Or something along those lines.
Subscripting arrays is the same as incrementing pointers
Equivalent expressions: A[i] = *(A+i)
The multiplication does occur but it's handled at compile time,
the actual value of an expression in the form pointer + integer is pointer + integer * sizeof ( type pointed by the pointer )