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 6836755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:23:39+00:00 2026-05-26T23:23:39+00:00

In a for-loop in C#, which of the following codeblocks are the best performance

  • 0

In a for-loop in C#, which of the following codeblocks are the best performance wise? Or is there no difference?
The diffenrence is whether the condition is saved in a local variable or read directly from an object.

Option 1

float maxDepth = 0;
int maxnumber = 0;

for (int i = 0; i < defects.Total; i++)
{
    if (defects[i].Depth > maxDepth)
    {
        maxDepth = defects[i].Depth;
        maxnumber = i;
    }
}

Option 2

float maxDepth = 0;
int maxnumber = 0

int defectNumber = defects.Total;
for (int i = 0; i < defectNumber; i++)
{
    if (defects[i].Depth > maxDepth)
    {
        maxDepth = defects[i].Depth;
        maxnumber = i;
    }
}

I’m sorry if this question have been asked a lot, but I couldn’t find it anywhere.

Anders

  • 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-26T23:23:39+00:00Added an answer on May 26, 2026 at 11:23 pm

    For all such questions you can use System.Diagnostics.Stopwatch like this.

    var stopwatch = new Stopwatch();
    stopwatch.Start();
    
    float maxDepth = 0; 
    int maxnumber = 0;  
    for (int i = 0; i < defects.Total; i++) 
    {    
        if (defects[i].Depth > maxDepth)     
        {         
            maxDepth = defects[i].Depth;         
            maxnumber = i;     
        } 
    } 
    
    stopwatch.Stop();
    Debug.WriteLine("Elapesed time for method 1: {0} msec.", stopwatch.ElapsedMilliseconds);
    stopwatch.Reset();
    stopwatch.Start();
    
    maxDepth = 0; 
    maxnumber = 0;  
    int defectNumber = defects.Total; 
    for (int i = 0; i < defectNumber; i++)     
    {    
        if (defects[i].Depth > maxDepth)     
        {         
            maxDepth = defects[i].Depth;         
            maxnumber = i;     
        } 
    } 
    stopwatch.Stop();
    Debug.WriteLine("Elapesed time for method 2: {0} msec.", stopwatch.ElapsedMilliseconds);
    

    Then you got exact information. (The best way is to take several runs and use the mean value)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a for-loop which performs the following function: Take a M by 8
I have the following command which will loop over all the subdirectories in a
I have the following loop inside an function init(); which is executed onload, I
i have a php loop which displays only one record even if there is
I've written the following code. I have a loop which iterates between two red
I have variable call items having array of items which contains following value [[#<Item
I have the following query which is inside a loop and is breaking SELECT
I have a function which does the following loop many, many times: for cluster=1:max(bins),
I have the following loop which generates a little snippet of html: for (var
Hey guys I have the following while loop which seems not to stop. It's

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.