Fastest Container

I want to hold a container with a structure containing different values of all data types. The structure has about 15 values (ranging from int to string) and the container will hold about 50 of these structures. I want to use a container so fast it's unbenchmarkable. :D What would my best option be right now (list/vector maybe?)
vector. It depends on what exactly you want to do with it, though.
The contents of the struct shouldn't matter since you should only be storing pointers to the objects. You ARE only storing pointers to them right?

What's "unbenchmarkable"? I am familiar with benchmarking but it seems like you might be confusing this term with something else.
It depends on what you wish to do. If you're going to be deleting elements halfway the container, a std::list is probably the best. If you need random access, you'll need a std::vector, but removing elements from non-last positions will be slower.

If by "unbenchmarkable" you mean "too fast to measure": that depends on the measurement function used. GetTickCount() (in windows.h) will only refresh every ~16ms, so anything below that is technically "unbenchmarkable". In any case, your choice of container will probably not be a deciding factor in your speed performance.
When I said "unbenchmarkable" I was just joking, saying that I want it to be extremely fast. Sorry for the extremely late response by the way - I completely forgot I opened this topic. I was using an std::map and it caused a RIDICULOUS amount of lag in the program. Would vector be much faster or should I just use variables?

Before 'cleaning up' my code I was getting data like this..
1
2
3
4
5
6
7
8
9
10
11
12
class CGlobalData
{
public:
	void GetFloat( float &flReturn );
	// Many more GetData functions...
private:
	float flSpeed;
	float flHeight;
	float flWidth;
	float flTime;
	// Many more variables...
};

Which is obviously used like this...
1
2
float flSpeed;
CGlobalData::GetFloat( flSpeed );


I had absolutely no problem with speed using that code but, let's be honest, that is some UGLY programming. So I decided to clean it up like this..
1
2
3
4
5
6
7
class CGlobalData
{
public:
	template<class _Type> _Type GetGlobal( std::string Name );
private:
	std::map<std::string, float>Globals;
};

Which can be simply used like this..
 
float flSpeed = CGlobalData::GetGlobal<float>( "flSpeed" );


The program I am working with is quite large and throughout the entire program it is making a ridiculous amount of calls to GetGlobal, SetGlobal, etc., roughly 250-1000 times per second. Unfortunately after spending a fair bit of time cleaning up my entire program using this method, I realized it's HORRIBLY slow. So I'm looking for an alternative container without going back to using global variables (which I hate.)
Last edited on
Erm, nothing changed, you're still using global variables. Whether your values are in a global map or are separate variables is largely irrelevant in this aspect...
Exactly why do you need to be able to fetch a variable by name? Why not the most obvious CGlobalData::getSpeed() etc.?
If the set of values you have is fixed and you actually need to be able to retrieve a particular global value without knowing which one at compile-tile (for whatever strange reason), you can use integral constants as "names" and fetch the values from an array/vector via index. This is far faster than using a map with string as the key type.
CGlobalData::GetGlobal<float>( "flSpeed" );
HOLY ****!
it's HORRIBLY slow
I'll say.

The problem with a global object is not the fact itself that the object is global. It's that it can be accessed from anywhere without any control or structure. Your "clean up" removes the global but not the problem associated with it, and it adds a HUGE overhead to accessing the object.
Using a map would make sense if you wanted to be able to add more variables at run time, but the code it's replacing in this case implies that that's obviously not something you need.

I don't know what those variables are holding or how your program uses them, but sometimes data just needs to be global because it's the only thing that makes sense without getting silly with the parameter passing. A good example are program options, which often need to be accessed all over the place.

Oh, and if you find yourself doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private:
	T foo;
	U bar;
	V baz;
	//...
public:
	const T &get_foo(){ return foo; }
	const U &get_bar(){ return bar; }
	const V &get_baz(){ return baz; }
	//...
	void set_foo(const T &a){ foo=a; }
	void set_bar(const U &a){ bar=a; }
	void set_baz(const V &a){ baz=a; }
	//... 
It's probably because you're doing something wrong. Probably around the "private" part.
Last edited on
@Athar
I designed the globals class this way, more than anything, because it was nice and neat. My program has over 200 variables, so having a class with 200 private variables and 200 GetSpecificVariable() functions was completely out of the question (even just 200 public variables is ridiculous.) I opted instead to use a mapped array to store the variables and set them up a single time in the constructor, which wasn't nearly as 'ugly' as the previous code.

@Helios
Actually I did fix the 'global' problem. First of all it wasn't ever technically a global, but I just had hundreds of public variables in a class being accessed several hundred times a second. I switched the public variables to a private mapped array with templates set up for accessing and changing them. The program only creates the variables a single time but they are not only accessed, but changed as well, several hundred times a second during runtime.

@Athar
After reading your post it's blindingly obvious why my program was running like a slug after making the switch. With the program accessing the map as often as it was, using a string for the access key was probably a horrible idea. What a rookie mistake, I definitely feel like my username right now. :D
Last edited on
My program has over 200 variables, so having a class with 200 private variables and 200 GetSpecificVariable() functions was completely out of the question (even just 200 public variables is ridiculous.)
I agree with the former, not with the latter. 200 public variables would have been preferable. If you had used, say, a vector, you'd have ended up doing something like this:
1
2
3
4
std::vector<float> v(200);
const size_t flSpeed_offset=0,
    flHeight_offset=1,
    //... 
I fail to see how this is better.

First of all it wasn't ever technically a global, but I just had hundreds of public variables in a class [...] I switched the public variables to a private mapped array with templates set up for accessing and changing them.
Technicalities. If it can be accessed from anywhere in the program, then it's the same as a global, even if it's not called a global.
Topic archived. No new replies allowed.