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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:27:14+00:00 2026-06-10T08:27:14+00:00

Possible Duplicate: for loop optimization Let’s say we want to loop over the characters

  • 0

Possible Duplicate:
for loop optimization

Let’s say we want to loop over the characters of a string s. I’d say the following code is more or less the default.

for( int i = 0; i < s.length(); i++ ) {
    doSomethingWith( s.charAt( i ) );
}

Question #1: Why do I often see the following?

final int length = s.length();
for( int i = 0; i < length; i++ ) {
    doSomethingWith( s.charAt( i ) );
}

At first sight this seems reasonable since the inequality is evaluated on every iteration. I would however expect the VM to optimize this anyway, since strings are immutable. Any thoughts? And what if we iterate over a mutable structure (that’s not referenced by any other thread)? And what if length() is not guaranteed to run in O(1)?

Question #2: Some people seem to think substituting ++i for i++ speeds up the code. Are they right? Again this is not what I’d expect, but I’m simply not sure.

We all know not to optimize prematurely. At the same time, if we can produce slightly faster code at hardly any expense, we’d be silly not to. Of course one could argue that both “optimizations” hurt readability, but in my opinion the damage is so small that it would be justified in certain circumstances.

I tried to measure any differences in performance, but it’s hard to get conclusive results. Although this should settle it for any specific application, I’m aiming for insight and a general answer here.

(Although I wrote this with the HotSpot VM in mind it could be interesting also to consider other platforms, like mobile devices.)

  • 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-10T08:27:15+00:00Added an answer on June 10, 2026 at 8:27 am

    Question #1: Why do I often see the following?

    The first example is a premature optimization. The reason people do it is that they often don’t profile their code to see where the real bottlenecks are, and instead just try to guess.

    And what if length() is not guaranteed to run in O(1)?

    Then it would make more sense to make this optimization. If you know that calculating the length is an expensive operation and that the value won’t change, then calculating it once and storing the result could potentially improve performance.

    Question #2: Some people seem to think substituting ++i for i++ speeds up the code. Are they right? Again this is not what I’d expect, but I’m simply not sure.

    They aren’t right. Their argument is that i++ has to create a temporary copy of the variable whereas ++i does not. This is something that used to be true in C or C++ a very long time ago when compilers were not good at optimizing. It is not true for Java.

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

Sidebar

Related Questions

Possible Duplicate: java for-loop problem Why is the output of following code: for (float
Possible Duplicate: for loop optimization In java i have a block of code: List
Possible Duplicate: Loop code for each file in a directory I assumed that I
Possible Duplicate: php loop through associative arrays i have this piece of code which
Possible Duplicate: Breaking out of a nested loop I have this code foreach (___)
Possible Duplicate: Advantages of std::for_each over for loop So I was playing around with
Possible Duplicate: Safest Way to Loop Over Javascript Object I have this hard coded
Possible Duplicate: For loop and if statement Hi everyone, i have a following for
Possible Duplicate: PHP code to remove everything but numbers i have a loop that
Possible Duplicate: Javascript for loop and setTimeout issue I want this loop to change

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.