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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:42:28+00:00 2026-05-20T08:42:28+00:00

i wonder why is this happening all the time…!! I wrote two programs one

  • 0

i wonder why is this happening all the time…!!
I wrote two programs one in c and the other in c++.. Both performs the same action. i.e prints numbers from 1 to 2000000. Also i am setting the timer at the begtinning of execution.. and after printing all the numbers time elapsed is also printed..
The time elapsed for a c++ program is always greater than for a c program.
i feel the difference in time is significant. I am curious to know what is the cause for this..????..

Here are the two programs

//iotest.c

#include<stdio.h>
#include<time.h>

clock_t start=clock();

int main()
{
for(int i=0;i<2000000;i++)

printf("%d\n",i);

printf("Time Elapsed: %f\n",((double)clock()-start)/CLOCKS_PER_SEC);

return 0;

}

//iotest.cpp

#include<iostream>

#include<time.h>

using namespace std;

clock_t start=clock();

int main()
{
    for(int i=0;i<2000000;i++)

    cout<<i<<endl;

    cout<<"Time elapsed "<<((double)clock()-start)/CLOCKS_PER_SEC<<endl;

    return 0;

}

// ver C++ 4.3.2
Compiling c program by issuing the command

g++ iotest.c

Execution gives

1

.

.

2000000

Time Elapsed: 5.410000(Not always same..)

Executing the second program

1

.

.

2000000

Time elapsed: 5.81(Not always same..)

  • 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-20T08:42:29+00:00Added an answer on May 20, 2026 at 8:42 am

    The difference between the two programs is that the C++ version uses endl, which not only inserts a newline but flushes the buffer. The slow part of doing any output is flushing the buffer.

    Both programs would probably be about the same speed if you made your C++ program use

    count << i << "\n";
    

    The following two programs achieve a similar execution time:

    C-program (a.c):

    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
        clock_t start=clock();
        for (int i=0; i<2000000; i++) printf("%d\n",i);
        clock_t end=clock();
    
        fprintf(stderr, "Time Elapsed: %f\n",((double)end-start)/CLOCKS_PER_SEC);
        return 0;
    }
    

    Compile with: gcc -O3 -std=c99 a.c

    C++-program (b.cpp):

    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
        clock_t start=clock();
        for (int i=0;i<2000000;i++) cout << i << '\n';
        clock_t end=clock();
    
        cerr << "Time elapsed " << ((double)end-start)/CLOCKS_PER_SEC << endl;
    
        return 0;
    }
    

    Compile with g++ -O3 b.cpp

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

Sidebar

Related Questions

I wonder if this affects performance or memory consumption a lot. I need an
i wonder what this query means: $blogPost = Doctrine_Query::create()->from('BlogPost p')->where('p.slug = ?', 'doctrine-released')->execute(); could
I wonder is this a bug in chrome or jQuery, or am I misunderstanding
I was wonder if this would be possible and how to do it. I
I really wonder why this question hasn't popped up here so far. I have
Hi (sorry for my ugly english) I wonder if this is possible to retrieve
Possible Duplicate: What does new() mean? Like in title. I wonder what this syntax
I wonder how is this possible in groovy to start an array from the
This is more a 'wonder why' than a specific issue but look at the
I wonder how to get something like this: Write copy(a, b, 2, 3) And

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.