使用临时变量或直接从数组更有效?

发布于 2024-11-27 06:18:00 字数 565 浏览 0 评论 0原文

每次使用变量时访问数组是否更有效,或者创建一个临时变量并将其设置为数组:

例如:

int A; int B; ...etc... int Z;
int *ints = [1000 ints in here];
for (int i = 0; i < 1000; i++) {
    A = ints[i];
    B = ints[i];
    C = ints[i];
    ...etc...
    Z = ints[i];
}

或者

int A; int B; ...etc... int Z;
int *ints = [1000 ints in here];
for (int i = 0; i < 1000; i++) {
    int temp = ints[i];
    A = temp;
    B = temp;
    C = temp;
    ...etc...
    Z = temp;
}

是的,这不是我想要做的事情,但这是我最简单的例子能想到的。

那么哪个 for 循环使用数组会更快呢?

Is it more efficient to access an array each time I use a variable, or to create a temporary variable and set it to the array:

For example:

int A; int B; ...etc... int Z;
int *ints = [1000 ints in here];
for (int i = 0; i < 1000; i++) {
    A = ints[i];
    B = ints[i];
    C = ints[i];
    ...etc...
    Z = ints[i];
}

or

int A; int B; ...etc... int Z;
int *ints = [1000 ints in here];
for (int i = 0; i < 1000; i++) {
    int temp = ints[i];
    A = temp;
    B = temp;
    C = temp;
    ...etc...
    Z = temp;
}

Yes, this is not something I want to do, but it is the easiest example I could think of.

So which for loop would be quicker at using the array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧街凉风 2024-12-04 06:18:00

没关系;编译器很可能在这两种情况下生成相同的代码(除非您禁用了所有优化)。 (生成的汇编代码可能类似于第二个示例 - 首先,数组元素将被加载到寄存器中,然后,每当需要数组元素时就会使用该寄存器。)采用您认为最易读的样式并且最不容易出错(可能是后一种风格,可以避免重复索引)。

(这假设您没有任何线程或易失性变量,以便保证数组元素在循环迭代过程中不会更改。)

It doesn't matter; the compiler will most likely produce the same code in both cases (unless you have disabled all optimizations). (The generated assembly code will likely resemble the second example - first, the array element will be loaded into a register, and then, the register will be used whenever the array element is needed.) Go with the style you find to be most readable and least prone to errors (which is probably the latter style, which avoids repeating the index).

(This assumes that you don't have any threads or volatile variables, so that the array element is guaranteed not to change in the course of a loop iteration.)

怎樣才叫好 2024-12-04 06:18:00

编译器足够聪明,可以意识到它们是等效的,并且会生成相同的代码。因此,您应该以最容易理解的方式编写它,以便将来的人阅读您的代码。

The compiler is smart enough to realize that these are equivalent and will produce the same code. You should therefore write it in the most understandable way for future people reading your code.

蓝颜夕 2024-12-04 06:18:00

正如 Aasmund 的回答所述,可能没有性能差异,因为编译器将以相同的方式处理两者。但是,您可能会发现分配给临时变量可以提高代码的可读性,并且如果将来您想在整个循环中使用 ints[i+1] ,您只需要更改一行而不是许多。不过,永远不要将变量称为“temp”,而是给它一个有用的名称,例如currentInt

As Aasmund's answer states, there is likely no performance difference since the compiler will treat both in the same way. However, you might find assigning to a temporary variable gives improved code readability, and if in the future you want to use ints[i+1] throughout the loop you will only need to change one line rather than many. Never call a variable "temp" though, give it a useful name like currentInt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文