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

  • Home
  • SEARCH
  • 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 30457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:27:03+00:00 2026-05-10T13:27:03+00:00

Which of the following has the best performance? I have seen method two implemented

  • 0

Which of the following has the best performance?

I have seen method two implemented in JavaScript with huge performance gains, however, I was unable to measure any gain in C# and was wondering if the compiler already does method 2 even when written like method 1.

The theory behind method 2 is that the code doesn’t have to access DataTable.Rows.Count on every iteration, it can simple access the int c.

Method 1

for (int i = 0; i < DataTable.Rows.Count; i++) {     // Do Something } 

Method 2

for (int i = 0, c = DataTable.Rows.Count; i < c; i++) {     // Do Something } 
  • 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. 2026-05-10T13:27:03+00:00Added an answer on May 10, 2026 at 1:27 pm

    No, it can’t do that since there is no way to express constant over time for a value.

    If the compiler should be able to do that, there would have to be a guarantee from the code returning the value that the value is constant, and for the duration of the loop won’t change.

    But, in this case, you’re free to add new rows to the data table as part of your loop, and thus it’s up to you to make that guarantee, in the way you have done it.

    So in short, the compiler will not do that optimization if the end-index is anything other than a variable.

    In the case of a variable, where the compiler can just look at the loop-code and see that this particular variable is not changed, it might do that and load the value into a register before starting the loop, but any performance gain from this would most likely be negligible, unless your loop body is empty.

    Conclusion: If you know, or is willing to accept, that the end loop index is constant for the duration of the loop, place it into a variable.


    Edit: Re-read your post, and yes, you might see negligible performance gains for your two cases as well, because the JITter optimizes the code. The JITter might optimize your end-index read into a direct access to the variable inside the data table that contains the row count, and a memory read isn’t all that expensive anyway. If, on the other hand, reading that property was a very expensive operation, you’d see a more noticable difference.

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

Sidebar

Related Questions

I have a static utility class, FileUtils which has the following method: public static
I've one interface which has following method signatures: public interface ITag { int M_Id
I have list in python which has following entries name-1 name-2 name-3 name-4 name-1
I have a Spring Batch process which has following kind of code. <step id=step1
i have a string which has the following format (3,1,Mayer,Jack). I need all 4
I have a CSV file which has the following format: id,case1,case2,case3 Here is a
I have an sql table which has the following rows. (4081, 3, '', 'contrapreneurship.jpg',
i have the following code which has some duplication private List<SelectListItem> GetDeskList(int deskId) {
I have home.html which has the following div <div id=feature class=feature> </div> I have
I have a text file which has the following structure: 341|18 Hello world|20090225230048AAnhStI|90|$0.30|10|289|2|2|2|Is that

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.