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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:14:27+00:00 2026-05-22T12:14:27+00:00

Consider the example with for loop: for(int i = 0; i <= NUM; i++);

  • 0

Consider the example with for loop:

for(int i = 0; i <= NUM; i++);  // forward
for(int i = NUM; i >= 0; i--);  // reverse

I tested this loops with gcc (linux-64). Without any optimization flag, forward loop was faster and with optimization to O3/O4, reverse loop was faster.

Somewhere I heard that due to better cache replacement techniques, forward loop is faster.

Personally I think, reverse loop should be faster (whether NUM is a constant or variable). Because any microprocessor will have single instruction for comparison with 0, i >= 0 (i.e. JLZ (jump if less than zero) and equivalent).

Is there any deterministic answer to this ?

  • 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-22T12:14:27+00:00Added an answer on May 22, 2026 at 12:14 pm

    No, there is absolutely no deterministic answer for this. You’re looking at two different levels of abstraction.

    C++ has absolutely nothing to say about what happens under the covers, performance-wise. It specifies a virtual machine which executes C++ code and, while it covers functionality, it does not cover performance of the underlying environment (a).

    Which of those is faster will depend on a variety of factors. You may find yourself running on a CPU which makes no distinction between comparing with an arbitrary value and comparing with zero.

    You may find an architecture where incrementing a register is ten times faster than decrementing one, bizarre though that may seem.

    You may even find a brain-dead architecture that has no decrement, add or subtract instructions at all, and you have to emulate decrement by calling increment 2n-1 times (where n is the word size).

    Bottom line: you can’t presume to know what’s going on under the hood unless you want to look at a very specific CPU, compiler, etc.

    You should optimise your code for readability first. If you need to process things in an increasing manner, use the first option. If a decreasing manner, use the latter. If either way seems equally natural, then choose the fastest one, discovered by benchmarking or analysis of the underlying architecture and assembler code. But only do this if you have a specific performance problem, otherwise you’re wasting effort.

    In any case, since you’re almost certainly going to be using i for something, it’s likely that whatever tiny increase in performance you get by going the fastest way will be more than swamped by the fact that you now have to calculate NUM-i inside the loop (unless, of course, the compiler is smarter than the developer which, based on what I’ve seen from gcc, is quite possible).


    (a) It does specify certain performance-related things such as the time complexity of some things in the containers library, but not specifically the thing you’re asking about, whether forward loops or reverse ones are faster.

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

Sidebar

Related Questions

Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Consider this example (typical in OOP books): I have an Animal class, where each
Consider this example: zh_Hant_HK format = yy'年'M'月'd'日' ah:mm Not sure if you can see
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Consider this contrived example: # Dispatch on value of fruit_kind: TYPE_A = :apple TYPE_B
Consider the following example program: next :: Int -> Int next i | 0
Consider this example SOAP Client script: $SOAP = new SoapClient($WDSL); // Create a SOAP
Consider this example The Interface interface IBusinessRules { string Perform(); } The Inheritors class
Consider this example showing the YUI tree in action: http://developer.yahoo.com/yui/examples/treeview/tv_edit.html Select the title in

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.