C ++映射真的很慢吗? [英] C++ map really slow?

查看:189
本文介绍了C ++映射真的很慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为gamemaker创建了一个dll。 dll的数组真的很慢,所以在问了一下我学到了,我可以使用地图在c ++和做一个dll。



无论如何,病态代表我需要存储在一个3d数组中:
information [id] [number] [number]
id对应到一个对象id。第一个数字字段的范围为0-3,每个数字表示不同的设置。第二个数字字段表示数字字段1中设置的值。
so ..

 信息[101] [1] [4]; 
信息[101] [2] [4];
信息[101] [3] [4];

这将转换为id为101的对象对于设置1,2和3的值为4 。
i这样做,尝试使用地图复制:

  //声明为类成员
地图< ; double,map< int,double> *> objIdMap;

/////在页面下方,在某些功能中
map< int,double> objSettingsMap;
objSettingsMap [1] = 4;
objSettingsMap [2] = 4;
objSettingsMap [3] = 4;
map< int,double> * temp =& objSettingsMap;
objIdMap [id] = temp;所以第一个地图,objIdMap存储id作为关键字,另一个指向另一个地图的指针,它存储表示设置为键的数字,设置的值作为值。



然而,这是一个游戏,所以新对象有自己的id和设置可能需要存储(有时每隔几秒就会有一百个新的),而现有的设置会不断地需要检索游戏每一步的值。地图是不是能处理这个?我有一个非常相似的事情与游戏制造商的数组,它的工作正常。

解决方案

1)你的代码是错误的:你存储指向本地对象 objSettingsMap 的指针,一旦超出范围就会被销毁。您必须存储地图obj,而不是指向它的指针,因此本地地图将被复制到此对象。



2)地图可能变得任意大(我有地图数百万条记录)。如果你需要速度尝试hash_maps(C ++ 0x的一部分,但也可以从其他来源),这是更快。但每秒增加一百个条目不应该是一个问题。但是对于执行速度,你应该总是使用分析器来担心执行速度。



3)我不太确定你的嵌套结构是否必须是映射。根据您拥有的设置数量及其可能具有的值,结构或位域或向量可能更准确。


i've created a dll for gamemaker. dll's arrays where really slow so after asking around a bit i learnt i could use maps in c++ and make a dll.

anyway, ill represent what i need to store in a 3d array: information[id][number][number] the id corresponds to an objects id. the first number field ranges from 0 - 3 and each number represents a different setting. the 2nd number field represents the value for the setting in number field 1. so..

information[101][1][4]; 
information[101][2][4]; 
information[101][3][4]; 

this would translate to "object with id 101 has a value of 4 for settings 1, 2 and 3". i did this to try and copy it with maps:

//declared as a class member
map<double,  map<int, double>*> objIdMap;

///// lower down the page, in some function
map<int, double> objSettingsMap;
objSettingsMap[1] = 4;
objSettingsMap[2] = 4;
objSettingsMap[3] = 4;
map<int, double>* temp = &objSettingsMap;
objIdMap[id] = temp;

so the first map, objIdMap stores the id as the key, and a pointer to another map which stores the number representing the setting as the key, and the value of the setting as the value.

however, this is for a game, so new objects with their own id's and settings might need to be stored (sometimes a hundred or so new ones every few seconds), and the existing ones constantly need to retrieve the values for every step of the game. are maps not able to handle this? i has a very similar thing going with game maker's array's and it worked fine.

解决方案

1) Your code is buggy: You store a pointer to a local object objSettingsMap which will be destroyed as soon as it goes out of scope. You must store a map obj, not a pointer to it, so the local map will be copied into this object.

2) Maps can become arbitrarily large (i have maps with millions of entrys). If you need speed try hash_maps (part of C++0x, but also available from other sources), which are considerably faster. But adding some hundred entries each second shouldn't be a problem. But befre worring about execution speed you should always use a profiler.

3) I am not really sure if your nested structures MUST be maps. Depending of what number of setting you have, and what values they may have, a structure or bitfield or a vector might be more accurate.

这篇关于C ++映射真的很慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆