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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:35:17+00:00 2026-06-13T01:35:17+00:00

I am trying to optimize a chunk of code where speed is very important

  • 0

I am trying to optimize a chunk of code where speed is very important and wondered if checking the int that holds the number of times a for loop is about to loop and not doing the for loop if it is equal to zero was any faster or slower than just letting the for loop execute 0 times.

I realize that any speed improvement would be tiny; it just started to become more of a curiosity. Also would this be different from Java to say C++ or C?

Example:

size=0;
for (int i = 0;i<size;i++)
{
}

or

size=0;
if (size!=0)
{ 
    for (int i = 0;i<size;i++)
    {
    }
}

Of course, in the real code the size is often not zero, but when it is which would be faster if either?

  • 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-13T01:35:20+00:00Added an answer on June 13, 2026 at 1:35 am

    Academically? Yes, since every time the size isn’t 0, it will run 1 extra comparison.

    Realistically? No. When you’re talking comparisons like this, you’re counting nanoseconds, especially after the JVM creates the machine language and it gets executed directly on the processor.

    Now, which one should you use. Probably the first one. It doesn’t save time, and it’s shorter and cleaner. Even better, Java has a for-each loop construct that makes it so you don’t have to have the index at all:

    String[] strs = ... ;
    for (String str : strs) {
        // Do something with str
    }
    

    For arrays, this compiles to an indexed for-loop. For Collection objects:

    Collection<String> strs = ... ;
    for (String str : strs) {
        // Do something with str
    }
    

    This one compiles to a for loop with an Iterator declaration.

    These are the best option if you don’t need the index itself, since it compiles to the same thing after compilation, it’s clear, and it’s the same for both arrays and collections.

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

Sidebar

Related Questions

I'm trying to optimize code that I have. In order to do that, I
I'm trying to optimize some code and I want to instead on checking a
I am trying to optimize a piece of .NET 2.0 C# code that looks
I'm trying to optimize some existing code ( where speed is the primary criteria
I'm trying to optimize some code that takes some test data stored in CSV
I'm trying to optimize my code using Neon intrinsics. I have a 24-bit rotation
I'm trying to optimize the size of my Delphi classes so that they take
I'm trying to optimize my C++ code. I've searched the internet on using dynamically
Basically I'm trying to optimize my jQuery code by unbinding elements anytime is necessary,
In trying to optimize C and LISP, looking at the assembler code output by

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.