Stl containers Need Fastest Read and write, non random

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
Is one container static? If so, do you really need to put the random values in a container or can you compare them directly to the static container?

Or just stick the values in an array and do a memcmp(). Nothing will beat that for speed.
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.
how bout a different container type?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//generate random sequence NUMBS = 8 MAXEACH = 8//
do
{
		for(int i = 0; i < NUMBS; i++)		
		{
			array1[i] = rand_0toN1(MAXEACH) + 1;
		}

		//--------compare tickets element by element-----no more-------
	
		//using memcmp to compare arrays.
                      //sizeofint1 = NUMBS * sizeof(unsigned int) (amount in array)*(size of unsigned int)

		if(memcmp(array1,array2, sizeofint1) == 0)
		{
			gotit = true;
			cout << "gotit" << endl;
			break;
		}
                 x++;	//increment global counter
}while(x < 50000000 && gotit == false);




thats the setup now
Last edited on
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.

Regards
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.
Last edited on
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.
maybe they said that pointer incrementing was better then indexing as incrementin doesnt use the index multiplaction thing, getting kinda tired...
Last edited on
thanks guys
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 )
Last edited on
Topic archived. No new replies allowed.