替代嵌套数组(6维)与内存的差距保持O(1)访问 [英] Alternative to nested array (6 dimensions) with memory gaps retaining O(1) access
问题描述
我的统计数据,从运行不同配置的程序读取。比方说,有6个配置( A
, B
... ˚F
)。该配置可能不是线性变化,所以如果你想测量值作为一个表的,有可能是在表中的空白。现在的问题是关于构建这些统计数据在内存中。
I am reading in statistical data from running a program with different configurations. Let's say there are 6 configurations (a
, b
, ..., f
). The configurations may not change linearly, so if you think of the measurements as a table, there could be gaps in the table. The question is regarding structuring these statistical data in memory.
,想到的第一件事就是阅读这些配置动态分配的6深数组或数组:
The first thing that comes to mind is to read these configurations to a dynamically allocated 6-deep array or arrays:
struct data ******measurements;
现在这工作正常。你有很少的内存开销(只有那些将数据实际分配的配置)和访问时间 O(1)
。
Now this works fine. You have very little memory overhead (only the configurations that have data will actually be allocated) and the access time is O(1)
.
除了这个事实,大多数人不喜欢 ******
三分球,这有添加配置意味着添加尺寸阵列的下跌,这可能除非读取/写入到阵列封装在一个函数变得丑陋。 (写入已封装照顾分配必要的时候,所以这实际上也是没有这么大的交易)。
Besides the fact that most people don't like ******
pointers, this has the downside that adding configurations means adding dimensions to the array, which could get ugly unless read/write to the array is encapsulated in a function. (Write is already encapsulated to take care of allocation when necessary, so this in fact is not such a big deal).
我想到的另一个想法是使用地图结构配置
到结构数据
使用AVL树什么(我已经,以便有没有实现间接费用) 。这解决了扩展配置参数的问题,但会降低访问时间 O(日志(N))
。
Another idea that comes to mind is to use a map of struct config
to struct data
using an AVL tree or something (which I already have so has no implementation overheads). This solves the problem of extending the configuration parameters, but decreases access time to O(log(n))
.
测试的数量可以得到 0相当大(日志(N))
有所作为。
The number of tests could get considerably large for the O(log(n))
to make a difference.
我的问题是:用6深嵌套数组这里是否合理?或者是有这样做的更好的办法?
推荐答案
真正的性能瓶颈(除了浪费内存)都没有计算,但间接性和高速缓存缺失的数量,你会遇到。这些可以通过百因素占主导地位,以一千指数之类的东西的计算。所以,你最好的选择是减少迂回的数量和投入进位计算。至于我可以看到这个最好由你的散列索引6来完成。这会降低你的间接为两个,第一查找存储在哈希表中的值(可能是一个指针),然后获取您的感兴趣的直接的数据。
The real performance bottleneck (besides wasting memory) are not computations but the number of indirections and cache misses that you will encounter. These can dominate by a factor of hundred to thousand the computation of indices and stuff like that. So your best choice is to reduce the number of indirections and invest a bit into computation. As far as I can see this would best be done by hashing your 6 indices. This would reduce your indirections to two, first looking up the value (probably a pointer) that is stored in the hash table, and then fetching the data that your are interested in directly.
这篇关于替代嵌套数组(6维)与内存的差距保持O(1)访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!