问题描述
在某些性能关键程序(单线程)中,如果我具有原始类型的数组,并且需要在循环中多次访问相同的索引,则该
in some perf critical program (single threaded), if I have arrays of primitive types, and need to access the same index of those more than once in loops.
应该使用tmp变量,还是对数组进行常量间接寻址会更好/更快?
Should I use tmp variables, or would just constant indirection on the array be better/faster ?
我还可以想象,两者可能是相同的,也可能在编译时进行了透明优化.
I could imagine also that it is possible that either is the same / is transparently optimised at compile time.
推荐答案
在 general 中,对数组的访问比临时访问要慢,因为它有2个额外的开销
In general access to an array is slower than a temporary because it has 2 additional pieces of overhead
- 间接层
- 边界检查
但是,我将不会根据这些知识更改我今天拥有的任何代码,除非事件探查器清楚地表明紧密循环是我应用程序中的一个严重性能问题.而且还表明,更改为缓存的本地可产生明显的性能优势.
However I would not be changing any code I had today based on that knowledge unless a profiler clearly showed a tight loop to be a significant perf problem in my application. And furthermore showed that changing to a cached local produced a significant perf benefit.
这篇关于我应该在perf关键代码中使用C#中的数组上的变量还是多个间接寻址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!