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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:12:12+00:00 2026-05-16T03:12:12+00:00

I have the following two loops: #include <iostream> #include <stdlib.h> #include <time.h> using namespace

  • 0

I have the following two loops:

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;
int main(){

    int start=clock();
    for (int i=0;i<100;i++)
        cout<<i<<" "<<endl;
    cout<<clock()-start<<"\n"<<endl;
    cout<<"\n";

    int start1=clock();
    for (int j=0;j<100;++j)
        cout<<j<<" "<<endl;
    cout<<"\n";
    cout<<clock()-start1<<" \n "<<endl;

    return 0;
}

I ran that three times. On the first two runs, the second loop was fastest but, on the third run, the first loop was fastest. What does this mean? Which is better? Does it depend on the situation?

  • 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-16T03:12:12+00:00Added an answer on May 16, 2026 at 3:12 am

    In your case it is probably standard measurement error and it does not matter do you use post-increment or pre-increment. For standard types (int, byte …) it does not matter.

    BUT you should get used to using pre-increment because there are performance implications if you are using them on a Class, depending how those operators are implemented. Post-increment operator i++ has to make a copy of the object

    For example:

    class integer
    {
    public:
      integer(int t_value)
        : m_value(t_value)
      {
      }
    
      int get_value() { return m_value; }
    
      integer &operator++() // pre increment operator
      {
        ++m_value;
        return *this;
      }
    
      integer operator++(int) // post increment operator
      {
        integer old = *this; // a copy is made, it's cheap, but it's still a copy
        ++m_value;
        return old; // Return old copy
      }
    
    private:
      int m_value;
    

    Take a look at the following answer

    StackOverflow: i++ less efficient than ++i, how to show this?

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

Sidebar

Related Questions

I have the following two files :- single.cpp :- #include <iostream> #include <stdlib.h> using
I have the following two tables Groups Id (int) People Id (int) GroupId (int,
I have the following two entities: <?php namespace Site\AnnonceBundle\Entity; use Doctrine\ORM\Mapping as ORM; use
I have the following program test.cc: #include <iostream> unsigned char bogus1[] = { //
I have a problem with the following code: #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <iostream>
I have used the following code to create two threads: //header files #include <pthread.h>
I have the following two errors when using XMLReader. 1) Warning: XMLReader::read() [xmlreader.read]: MyXML.xml:43102:
I have the following program #include <stdio.h> main() { char ch[10]; gets(ch); printf(\nTyped: %s\n\n,
I have been trying to optimize the two following nested loops: def startbars(query_name, commodity_name):
I have following two arrays. I want the difference between these two arrays. 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.