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

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

问题描述

我为gamemaker创建了一个dll。 dll的数组,其中真的很慢,所以后来问我一点,我学会了我可以使用地图在C + +和做一个DLL。

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.

反正,我需要存储在一个3d数组:
information [id] [number] [number]
id对应到对象id。第一个数字字段的范围为0 - 3,每个数字代表不同的设置。
so ..

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];

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

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;

所以第一个地图,objIdMap存储id作为键,指向另一个地图的指针

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.

然而,这是一个游戏,所以新的对象有自己的id和设置可能需要存储(有时每几秒钟有一百个新的),现有的不断需要检索游戏的每一步的值。是地图无法处理这个?

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)你的代码是错误的:你存储指向一个局部对象 objSettingsMap 的指针,一旦超出范围就会被销毁。您必须存储地图obj,而不是指向它的指针,所以本地地图将被复制到此对象。

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)地图可以变得任意大有数百万条目)。如果你需要速度尝试hash_maps(C ++ 0x的一部分,但也可从其他来源),这是相当快。但是每秒增加几百条条目不应该是一个问题。但是,如果你担心执行速度,你应该总是使用一个profiler。

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)我不知道你的嵌套结构必须是地图。根据您拥有的设置数量以及它们可能具有的值,结构或位字段或向量可能更准确。

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天全站免登陆