Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cause for this difference..from C++ to C?

Tags:

c++

c

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..)

like image 589
Xplore Avatar asked Mar 03 '11 12:03

Xplore


People also ask

What causes low C?

C and C++ are now considered low-level languages because they have no automatic memory management.

What makes C different?

C is a statically typed programming language, which gives it an edge over other dynamic languages. Also, unlike Java and Python, which are interpreter-based, C is a compiler-based program. This makes the compilation and execution of codes faster.

What is the relationship between C and C++?

C is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming. Function and operator overloading is not supported in C. Function and operator overloading is supported by C++.


2 Answers

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

like image 97
Gabe Avatar answered Oct 01 '22 23:10

Gabe


You've started the clock at static-initialisation time, and your C++ standard library may have more work to do during static initialisation.

You're also flushing your stream repeatedly, and iostreams are a bit slow.

Do this:

#include <iostream>
#include <ctime>

int main()
{
    std::clock_t start = std::clock();
    for (int i=0;i<2000000;i++)
       std::cout << i << '\n';
    std::cout << "Time elapsed " << (static_cast<double>(std::clock()-start)/CLOCKS_PER_SEC) << std::endl;
    return 0;
}

A proper benchmark would involve you running this program a number of times and then taking the average of the results. The results you have are not at all reliable.

(Also notice how I changed time.h to its C++ counterpart, ctime.)

like image 37
Lightness Races in Orbit Avatar answered Oct 01 '22 21:10

Lightness Races in Orbit