简体   繁体   English

std :: tuple和std :: map的效率

[英]Efficiency of std::tuple and std::map

I am currently building a program that relies on multiple vectors and maps to retain previously computed information. 我目前正在构建一个程序,该程序依赖于多个矢量和地图来保留先前计算的信息。 My vectors are of the standard format, and not very interesting. 我的向量是标准格式,不是很有趣。 The maps are of the form 地图的形式

std::map<std::string, long double> 

with the intent of the string being a parseable mapping of one vector onto another, such as 字符串的意图是一个向量到另一个向量的可解析映射,例如

std::map<std::string, long double> rmMap("fvProperty --> fvTime", 3.0234);

where I can later on split the string and compare the substrings to the vector names to figure out which ones were involved in getting the number. 稍后,我可以在其中拆分字符串,并将子字符串与向量名称进行比较,以弄清楚涉及到数字的是哪些。 However, I have recently found that std::tuple is available, meaning I can skip the string entirely and use the vector positions instead with 但是,我最近发现std :: tuple可用,这意味着我可以完全跳过字符串,而使用向量位置代替

std::tuple<unsigned int, unsigned int, long double>

This allows me (as far as I can tell) to use both the first and the second value as a key, which seems preferable to parsing a string for my indices. 这使我(据我所知)可以将第一个值和第二个值都用作键,这似乎比为索引解析字符串更好。

My crux is that I am unaware of the efficiency here. 我的症结在于我不知道这里的效率。 There will be a lot of calls to these tuples/maps, and efficacy is of the essence, as the program is expected to run for weeks before producing an end result. 这些元组/映射将有很多调用,而功效至关重要,因为该程序预计将运行数周才能产生最终结果。

Thus, I would ask you if tuples are more or equally efficient (in terms of memory, cache, and cycles) than maps when it comes to large and computationally intense programs. 因此,我想问您在处理大型且计算密集的程序时,元组是否比映射更有效(在内存,缓存和周期方面)。

EDIT: If tuples cannot be used in this way, would the map 编辑:如果不能以这种方式使用元组,将地图

std::map<std::pair<unsigned int, unsigned int>, long double>

be an effective substitute to using strings for identification? 是使用字符串进行标识的有效替代方法?

The advantage of the map is to efficiently query data associated to a key. 映射的优点是可以有效地查询与键关联的数据。

It cannot be compared to tuples which just pack values together: you will have to travel tuples yourself to retrieve the correct value. 它不能与仅将值打包在一起的元组进行比较:您必须自己旅行元组才能检索正确的值。

Using a map<pair<unsigned int, unsigned int>, long double> as Mike suggests is probably the way to go. 按照Mike的建议map<pair<unsigned int, unsigned int>, long double>使用map<pair<unsigned int, unsigned int>, long double>是可行的方法。

Tuples and Maps are used for very different purposes. 元组和地图用于非常不同的目的。 So it should be primarily about what you want to use them for, not how efficient they are. 因此,应该主要是关于要使用它们的目的,而不是它们的效率。 You'll have some trouble to turn on your car with a screw driver, as you will have trying to fix a screw with your car keys. 使用螺丝刀打开汽车会遇到一些麻烦,因为您将不得不尝试用车钥匙固定螺丝。 I'd advice against both. 我建议两者都反对。

A tuple is one little dataset, in your case eg the set {1,2,3.0234} . 元组是一个小的数据集,例如您的集合{1,2,3.0234} A map is for mapping multiple keys to their values. 映射用于将多个键映射到它们的值。 In fact, a map internally consists of multiple tuples (pairs), each one containing a key and the associated value. 实际上,一个映射在内部由多个元组(对)组成,每个元组都包含一个键和关联的值。 Inside the map, the pairs are arranged in a way that makes searching for the keys easy. 在地图内部,这些对的排列方式使搜索键变得容易。

In your case, I'd prefer the map<pair<int, int>, double> as your edit suggests. 在您的情况下,如您的编辑建议,我更喜欢map<pair<int, int>, double> The keys (ie the vector index pairings) are much easier to search for and "parse" than those strings. 键(即矢量索引对)比那些字符串更容易搜索和“解析”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM