【发布时间】:2020-06-17 04:48:59
【问题描述】:
我目前正在研究多维数组迭代器的实现。考虑到两个连续范围(用于 std::equal、std::copy 目的)上的迭代,这些范围表示具有不同对齐方式的兼容数据(二维中的行与列主要),我想找到每个迭代器的步幅顺序给出最快执行时间。
例如:
row of vector components = A -> m elements
row of vectors = B -> n elements
2D plan of vectors = C -> 3 elements
row of plan of vectors = D -> 10 elements
given the datas ordered by ascending strides:
first array: B | A | C | D
second array: B | A | D | C
Obviously, we can iterate over both iterators by bunches of m*n elements. Then:
If we choose the first array convention, the first iterator is contiguous and the second one
will perform (3 - 1)*(10 - 1) jumps forward with a stride of 10 and (10 - 1) jumps backward.
If we choose the second array convention, the second iterator is contiguous and the first one will
perform (10 - 1)*(3 - 1) jumps forward with a stride of 3 and (3 - 1) jumps backward.
=> The second convention is better at everything in this example.
由于我必须考虑很多因素,例如内存来回、连续性和迭代器实现本身(这不是微不足道的),我想执行一个实验计划。但我也知道编译时的一切(大小和步幅),因此在编译时为每个模板实例化执行实验计划会很酷。我的问题是:
是否可以在编译时评估某些指令的运行时成本,在编译时除了输入数组的内存地址之外的所有内容都是已知的?
【问题讨论】:
-
你能提供示例代码吗?
-
我不能,因为实施将取决于答案。基本上,我只想说: std::copy(a.beginWith
(), a.endWith (), b.beginWith ()) -
只要指令运行时成本足够可重现,它是可能的。可靠的方法是在运行时对其进行评估,推导出一个数学定律(可能是一个愚蠢的查找表),然后在编译时使用该定律。
-
@Oliv:我可以认真对待你的提议,谢谢!如果您以可能的系统方法发布答案,我会给您我的赏金。找到适合外部测试程序输出的宏替换的预构建步骤会更好。
-
其实我是一名物理学家,我向你推荐的方法只是我工作的核心:en.wikipedia.org/wiki/Scientific_method。我从来没有用它来专门研究计算机作为物理对象。但我认为这是一种相关的方法,因为计算机行为对于理论家来说太复杂了。
标签: c++ arrays optimization iterator constexpr