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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:41:54+00:00 2026-06-12T14:41:54+00:00

Is there a difference between the following two code blocks in terms of the

  • 0
  1. Is there a difference between the following two code blocks in terms of the resulting machine code when using the llvm or gcc compilers?
  2. When is this optimization actually worthwhile, if ever?

Not optimized:

for (int i=0; i<array.count; i++) {
    //do some work
}

Optimized:

int count = array.count;
for (int i=0; i<count; i++) {
    //do some work
}

EDIT: I should point out that array is immutable and array.count doesn’t change during the loop’s execution.

  • 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-06-12T14:41:55+00:00Added an answer on June 12, 2026 at 2:41 pm
    1. You really need to check it yourself. My guess is that there is a difference in the emitted code, but it might depend on compiler and compiler options, and it certainly can depend on the definition of array.
    2. Nearly never, on the assumption that evaluating array.count is nearly always insignificant compared with “some work”. The way to measure it, though, is to use a profiler (or equivalent) and observe what proportion of your program’s runtime is spent at that line of code. Provided the profiler is accurate, that’s the most you could hope to gain by changing it.

    Suppose array.count is something really slow, that you happen to know will always return the same result but the compiler doesn’t know that. Then it might be worth manually hoisting it. strlen gets used as an example. It’s debateable how often strlen is actually slow in practice, but easy to manufacture examples likely to run slower than they need to:

    char some_function(char a) {
        return (a * 2 + 1) & 0x3F;
    }
    
    for (int i = 0; i < strlen(ptr); ++i) {
        ptr[i] = some_function(ptr[i]); // faster than strlen for long enough strings.
    }
    

    You and I know that some_function never returns 0, and hence the length of the string never changes. The compiler might not see the definition of some_function, and even if it does see the definition might not realize that its non-zero-returningness is important.

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

Sidebar

Related Questions

Is there any practical difference between the following two code snippets: NSObject * obj
Is there any difference between the following two lines of code in c# and
Is there a performance difference between the following two pieces of code? if (myCondition)
Is there any kind of difference between the following two lines of code in
Is there any execution speed difference between the following two lines of code? I
Is there any difference between following two ways of creating an object. Student s1
Is there any difference between the following two examples and should one be preferred
Is there a difference between the following two : ArrayList list = getData(); public
Is there a difference between the following two statements: mysql> EXPLAIN SELECT IF(arms IS
Will there be a difference between the following two ways of calling a function

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.