↻ back to index

Lab 2: Comparing Languages

This lab demonstration seeks to compare the performance of two programming languages: the C language and its high-level equivalent C++ language. Often lower-level languages like C are primitive and therefore close to the machine. This leads to the possible conclusion that C allows greater performance due to lower complexity. Will this be proven here?

C++ Language Test

On of the best ways to test the performance of a computer languages is to code a sorting algorithm and test it. In order to do this, a program called bubsort.cpp was downloaded off of Prof. Gomez's website, compiled, and tested. The code is shown down below.

This code was compilied with the g++ compiler.

g++ bobsort.cpp -o bubsort

Afterwards, the resulting program was executed 4 times with the average recorded. Afterwards the code was compiled with the highest level of optimization.

g++ -O3 bubsort.cpp -o bubsort2

Afterwards, it was tested 4 times.

C Language Test

bubsort was translated to C with minor changes and compiled as a C program as cbubsort.c. The program was also tested 4 times. This is the resulting code:

Run Time Speed of Programs compiled (in seconds)

1st test 2nd test 3rd test 4th test Average
C++ (normal) 11.966 12.114 11.976 12.010 12.017
C++ (max opt.) 1.861 1.886 1.875 1.856 1.865
C 9.2804 9.2804 9.2804 9.2804 9.2804

Conclusion

When comparing nominal compilations, the C language program performed faster then C++. This proves that C has less complexity and therefore runs faster than C++. But when compiled with maximum optimization (-O3), the resulting program way outperformed the C program.

What can be taken away from this is yes C++ is a more complex language than C but it can still outperform C with max optimization. Also, C++ gives programmers more tools to create algorithms than C does like vectors and strings. Overall, C++ is the better language.