C Vs C++ Vs C++14 (A Running time comparison)

Sameer Kathal
Nybles

--

While programming , sometimes you may have been stuck in a position as when you have choice of doing that question in C and C++ or C++14 , then the deciding factor comes to running time and the ability of the program to adapt to changes in the future . Here C++,C++14 have an edge over C as it has super predefined functions in its library called Standard Template Library popularly known as STL which is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc . It is a library of container classes, algorithms and iterators which saves tons of time by providing various data structures to the programmer at a click.

The major difference between C and C++ is that C is a procedural programming language and does not support classes and objects , while C++ is a combination of both procedural and object oriented programming language ; therefore C++ can be called a hybrid language.

Now coming to the point , you may have heard that C++ is slower than C but have you seen the stats ? After doing slight modifications in program we can make a C++ program run in same time or sometimes even faster than a corresponding program written in C . Here I’ve taken 2 questions on Binary Search and then coded them in C and C++ and C++14 and then compared their running time.

If you are generally using cin/cout in C++ then the following modification may be quite helpful . Just include it in the beginning of the program and use it in main and you are good to go.

You can find what it does here .

Q1 . The Soap Mystery

It is a general binary search question in which q queries are given and we have to binary search q number of times on n elements and its complexity id O(Q*Log(N)) where N is no of elements in the array.

You can find the solution here.

When coding it and running it on large test cases this is what i got.

Running time of Q1 using G++5.4.0

You can clearly see that C is much faster than C++14 but the thing to notice is that when using that modification(boost) it can be clearly seen that C++14 and C++ outperforms C in their execution time (in case of large test cases).

Q2 . Starting Game Development

This is an easy-medium question which involves doing N binary searches on a 2-d array with N rows and M columns and then finding the minimum of the answer obtained in each row which is our final answer. Complexity turns out to be O(Nlog(M)).

Solution can be found here .

Now coding it on 3 different languages here is what i got:

Running time of Q2 using G++5.4.0

Here also one can clearly see that C outperformes C++14 but using that modification , C++ is faster than C.

Some even use getchar() unlocked (though it has a security leak) , which is fastest among all of these as it directly reads and writes using register bypassing the streams in between (only use if time is a great concern) . Sometimes time bounds on questions are so strict that these modifications play a crucial rule , without them one may get TLE.

The compiler also plays a crucial role . Two of them are G++5.4.0 and CLANG . A Compiler is a computer program (or a set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language), with the latter often having a binary form known as object code.The compiler which can do this job faster will be better .

PROS OF GCC

1. GCC supports languages that clang does not aim to , such as Java , Ada , FORTRAN , Go , etc.

2. GCC supports many language extensions , some of which are not implemented by Clang . For instance , in C mode , GCC supports nested functions and has an extension allowing VLAs in structs.

PROS OF CLANG

  1. Clang is much faster and uses far less memory than GCC.
  2. Clang’s support for C++ is more compliant than GCC’s in many ways.
  3. Clang supports many language extensions, some of which are not implemented by GCC . For instance , Clang provides attributes for checking thread safety and extended vector types.
  4. Clang has been designed from the start to provide extremely clear and concise diagnostics (error and warning messages) , and includes support for expressive diagnostics.

So this concludes my little fun activity of comparing the execution time between two most commonly used languages in computer programming using binary search as a pivot . I hope you have gained some knowledge by reading this and will be able to choose the programming language in a better way.

Happy reading !

--

--

Sameer Kathal
Nybles
Writer for

SWE in morning , Stan at night. Tech enthusiast