Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6015155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:47:43+00:00 2026-05-23T02:47:43+00:00

I’m writing very processor-intensive cryptography code (C#), so I’m looking for any performance gains,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:47:44+00:00Added an answer on May 23, 2026 at 2:47 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am writing an app with both english and french support. The app requests
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.