将变量声明移到循环之外真的会提高性能吗?

发布于 2024-11-15 06:13:12 字数 434 浏览 8 评论 0原文

我正在编写非常处理器密集型的加密代码 (C#),因此我正在寻找任何性能提升,无论多么小。我听到过关于这个问题的两种观点。

对此有任何性能优势吗

int smallPrime, spGen;

for (int i = 0; i < numSmallPrimes; i++)
{
    smallPrime = smallPrimes[i];
    spGen = spHexGen[i];

    [...]
}

for (int i = 0; i < numSmallPrimes; i++)
{
    int smallPrime = smallPrimes[i];
    int spGen = spHexGen[i];

    [...]
}

编译器已经这样做了吗?

I'm writing very processor-intensive cryptography code (C#), so I'm looking for any performance gains, no matter how small. I've heard opinions both ways on this subject.

Is there any performance benefit at all to

int smallPrime, spGen;

for (int i = 0; i < numSmallPrimes; i++)
{
    smallPrime = smallPrimes[i];
    spGen = spHexGen[i];

    [...]
}

over this?

for (int i = 0; i < numSmallPrimes; i++)
{
    int smallPrime = smallPrimes[i];
    int spGen = spHexGen[i];

    [...]
}

Does the compiler do this already?

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

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

发布评论

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

评论(2

等风来 2024-11-22 06:13:12

根本没有任何性能优势。

所有局部变量都是在创建方法的堆栈帧时分配的,因此在方法中的何处声明它们并不重要。只是代码之间的变量范围有所不同,而且这只是编译器在编译时使用的信息。

编辑:

为了验证没有区别,我编译了这两种情况并检查了生成的机器代码,这两种情况是相同的:

在循环外声明变量:

            for (int i = 0; i < numSmallPrimes; i++) {
00000000  push        ebp 
00000001  mov         ebp,esp 
00000003  sub         esp,14h 
00000006  mov         dword ptr [ebp-4],ecx 
00000009  mov         dword ptr [ebp-14h],edx 
0000000c  cmp         dword ptr ds:[004214A8h],0 
00000013  je          0000001A 
00000015  call        69133CFB 
0000001a  xor         edx,edx 
0000001c  mov         dword ptr [ebp-10h],edx 
0000001f  xor         edx,edx 
00000021  mov         dword ptr [ebp-0Ch],edx 
00000024  xor         edx,edx 
00000026  mov         dword ptr [ebp-8],edx 
00000029  xor         edx,edx 
0000002b  mov         dword ptr [ebp-10h],edx 
0000002e  nop 
0000002f  jmp         0000006D 
                smallPrime = smallPrimes[i];
00000031  mov         eax,dword ptr [ebp-10h] 
00000034  mov         edx,dword ptr [ebp-14h] 
00000037  cmp         eax,dword ptr [edx+4] 
0000003a  jb          00000041 
0000003c  call        69136F00 
00000041  mov         eax,dword ptr [edx+eax*4+8] 
00000045  mov         dword ptr [ebp-8],eax 
                spGen = spHexGen[i];
00000048  mov         eax,dword ptr [ebp-10h] 
0000004b  mov         edx,dword ptr [ebp+8] 
0000004e  cmp         eax,dword ptr [edx+4] 
00000051  jb          00000058 
00000053  call        69136F00 
00000058  mov         eax,dword ptr [edx+eax*4+8] 
0000005c  mov         dword ptr [ebp-0Ch],eax 
                Console.WriteLine(smallPrime + spGen);
0000005f  mov         ecx,dword ptr [ebp-8] 
00000062  add         ecx,dword ptr [ebp-0Ch] 
00000065  call        68819C90 
            for (int i = 0; i < numSmallPrimes; i++) {
0000006a  inc         dword ptr [ebp-10h] 
0000006d  mov         eax,dword ptr [ebp-10h] 
00000070  cmp         eax,dword ptr [ebp-4] 
00000073  jl          00000031 
            }
        }
00000075  nop 
00000076  mov         esp,ebp 
00000078  pop         ebp 
00000079  ret         4

在循环内声明变量:

            for (int i = 0; i < numSmallPrimes; i++) {
00000000  push        ebp 
00000001  mov         ebp,esp 
00000003  sub         esp,14h 
00000006  mov         dword ptr [ebp-4],ecx 
00000009  mov         dword ptr [ebp-14h],edx 
0000000c  cmp         dword ptr ds:[006314A8h],0 
00000013  je          0000001A 
00000015  call        68FB3C5B 
0000001a  xor         edx,edx 
0000001c  mov         dword ptr [ebp-8],edx 
0000001f  xor         edx,edx 
00000021  mov         dword ptr [ebp-0Ch],edx 
00000024  xor         edx,edx 
00000026  mov         dword ptr [ebp-10h],edx 
00000029  xor         edx,edx 
0000002b  mov         dword ptr [ebp-8],edx 
0000002e  nop 
0000002f  jmp         0000006D 
                int smallPrime = smallPrimes[i];
00000031  mov         eax,dword ptr [ebp-8] 
00000034  mov         edx,dword ptr [ebp-14h] 
00000037  cmp         eax,dword ptr [edx+4] 
0000003a  jb          00000041 
0000003c  call        68FB6E60 
00000041  mov         eax,dword ptr [edx+eax*4+8] 
00000045  mov         dword ptr [ebp-0Ch],eax 
                int spGen = spHexGen[i];
00000048  mov         eax,dword ptr [ebp-8] 
0000004b  mov         edx,dword ptr [ebp+8] 
0000004e  cmp         eax,dword ptr [edx+4] 
00000051  jb          00000058 
00000053  call        68FB6E60 
00000058  mov         eax,dword ptr [edx+eax*4+8] 
0000005c  mov         dword ptr [ebp-10h],eax 
                Console.WriteLine(smallPrime + spGen);
0000005f  mov         ecx,dword ptr [ebp-0Ch] 
00000062  add         ecx,dword ptr [ebp-10h] 
00000065  call        68699BF0 
            for (int i = 0; i < numSmallPrimes; i++) {
0000006a  inc         dword ptr [ebp-8] 
0000006d  mov         eax,dword ptr [ebp-8] 
00000070  cmp         eax,dword ptr [ebp-4] 
00000073  jl          00000031 
            }
        }
00000075  nop 
00000076  mov         esp,ebp 
00000078  pop         ebp 
00000079  ret         4

There is no performance benefit at all.

All local variables are allocated when the stack frame for the method is created, so it doesn't matter where in the method you declare them. It's only the scope of the variables that differ between the codes, and that is only information that the compiler uses at compile time.

Edit:

To verify that there is no difference, I compiled the two cases and examined the generated machine code, and it is identical for the two cases:

Declaring variables outside the loop:

            for (int i = 0; i < numSmallPrimes; i++) {
00000000  push        ebp 
00000001  mov         ebp,esp 
00000003  sub         esp,14h 
00000006  mov         dword ptr [ebp-4],ecx 
00000009  mov         dword ptr [ebp-14h],edx 
0000000c  cmp         dword ptr ds:[004214A8h],0 
00000013  je          0000001A 
00000015  call        69133CFB 
0000001a  xor         edx,edx 
0000001c  mov         dword ptr [ebp-10h],edx 
0000001f  xor         edx,edx 
00000021  mov         dword ptr [ebp-0Ch],edx 
00000024  xor         edx,edx 
00000026  mov         dword ptr [ebp-8],edx 
00000029  xor         edx,edx 
0000002b  mov         dword ptr [ebp-10h],edx 
0000002e  nop 
0000002f  jmp         0000006D 
                smallPrime = smallPrimes[i];
00000031  mov         eax,dword ptr [ebp-10h] 
00000034  mov         edx,dword ptr [ebp-14h] 
00000037  cmp         eax,dword ptr [edx+4] 
0000003a  jb          00000041 
0000003c  call        69136F00 
00000041  mov         eax,dword ptr [edx+eax*4+8] 
00000045  mov         dword ptr [ebp-8],eax 
                spGen = spHexGen[i];
00000048  mov         eax,dword ptr [ebp-10h] 
0000004b  mov         edx,dword ptr [ebp+8] 
0000004e  cmp         eax,dword ptr [edx+4] 
00000051  jb          00000058 
00000053  call        69136F00 
00000058  mov         eax,dword ptr [edx+eax*4+8] 
0000005c  mov         dword ptr [ebp-0Ch],eax 
                Console.WriteLine(smallPrime + spGen);
0000005f  mov         ecx,dword ptr [ebp-8] 
00000062  add         ecx,dword ptr [ebp-0Ch] 
00000065  call        68819C90 
            for (int i = 0; i < numSmallPrimes; i++) {
0000006a  inc         dword ptr [ebp-10h] 
0000006d  mov         eax,dword ptr [ebp-10h] 
00000070  cmp         eax,dword ptr [ebp-4] 
00000073  jl          00000031 
            }
        }
00000075  nop 
00000076  mov         esp,ebp 
00000078  pop         ebp 
00000079  ret         4

Declaring variables inside the loop:

            for (int i = 0; i < numSmallPrimes; i++) {
00000000  push        ebp 
00000001  mov         ebp,esp 
00000003  sub         esp,14h 
00000006  mov         dword ptr [ebp-4],ecx 
00000009  mov         dword ptr [ebp-14h],edx 
0000000c  cmp         dword ptr ds:[006314A8h],0 
00000013  je          0000001A 
00000015  call        68FB3C5B 
0000001a  xor         edx,edx 
0000001c  mov         dword ptr [ebp-8],edx 
0000001f  xor         edx,edx 
00000021  mov         dword ptr [ebp-0Ch],edx 
00000024  xor         edx,edx 
00000026  mov         dword ptr [ebp-10h],edx 
00000029  xor         edx,edx 
0000002b  mov         dword ptr [ebp-8],edx 
0000002e  nop 
0000002f  jmp         0000006D 
                int smallPrime = smallPrimes[i];
00000031  mov         eax,dword ptr [ebp-8] 
00000034  mov         edx,dword ptr [ebp-14h] 
00000037  cmp         eax,dword ptr [edx+4] 
0000003a  jb          00000041 
0000003c  call        68FB6E60 
00000041  mov         eax,dword ptr [edx+eax*4+8] 
00000045  mov         dword ptr [ebp-0Ch],eax 
                int spGen = spHexGen[i];
00000048  mov         eax,dword ptr [ebp-8] 
0000004b  mov         edx,dword ptr [ebp+8] 
0000004e  cmp         eax,dword ptr [edx+4] 
00000051  jb          00000058 
00000053  call        68FB6E60 
00000058  mov         eax,dword ptr [edx+eax*4+8] 
0000005c  mov         dword ptr [ebp-10h],eax 
                Console.WriteLine(smallPrime + spGen);
0000005f  mov         ecx,dword ptr [ebp-0Ch] 
00000062  add         ecx,dword ptr [ebp-10h] 
00000065  call        68699BF0 
            for (int i = 0; i < numSmallPrimes; i++) {
0000006a  inc         dword ptr [ebp-8] 
0000006d  mov         eax,dword ptr [ebp-8] 
00000070  cmp         eax,dword ptr [ebp-4] 
00000073  jl          00000031 
            }
        }
00000075  nop 
00000076  mov         esp,ebp 
00000078  pop         ebp 
00000079  ret         4
耀眼的星火 2024-11-22 06:13:12

不是真的,编译器会为你做优化。

Not really, the compiler will do that optimization for you.

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