C vs C++
When I was learning to program C at the university, I started having an strong love for its simplicity, and defending it generally in front of C++ enthusiast, with the argument of C++ beeing messy and hidding much of the work behind the scenes. During this time I was reading this article by Bjarne Stroustrup and was espacially astonished by the factor which C++ was ahead in the efficiency section.
Some time has passed and I was generally corious to test the code myself. The page beeing some years old (from 1999) has a broken link to the source code. So, typing everything in my editor, here are results on my computer.
Sorting the doubles
First I generated the test files,
python -c 'import random; [print(2**31*random.random()) for i in range(10000)]' > rand10000.txt
python -c 'import random; [print(2**31*random.random()) for i in range(50000)]' > rand50000.txt
python -c 'import random; [print(2**31*random.random()) for i in range(100000)]' > rand100000.txt
python -c 'import random; [print(2**31*random.random()) for i in range(500000)]' > rand500000.txt
,then compiled and run them,
gcc -o c -O2 fileSortNums.c
g++ -o cpp -O2 fileSortNums.cpp
/usr/bin/time -v ./c rand10000.txt
/usr/bin/time -v ./c rand50000.txt
/usr/bin/time -v ./c rand100000.txt
/usr/bin/time -v ./c rand500000.txt
/usr/bin/time -v ./cpp rand10000.txt
/usr/bin/time -v ./cpp rand50000.txt
/usr/bin/time -v ./cpp rand100000.txt
/usr/bin/time -v ./cpp rand500000.txt
Resulting in this results:
| Example size | Time [s] C | Time [s] C++ | TimeC/C++ | Memory [MiB] C | Memory [MiB] C++ | Memory C/C++ |
|---|---|---|---|---|---|---|
| 10000 | 2.26 | 1.60 | 1.41 | 1644 | 3428 | 0.48 |
| 50000 | 64.26 | 44.55 | 1.44 | 2428 | 3516 | 0.69 |
| 100000 | 282.71 | 182.56 | 1.55 | 3860 | 3940 | 0.98 |
The 500000 double's file took way to much time on my machine, so I skipped it.
Replacing qsort with QSORT
Look pretty bad for the performance of C, ah?
The slitly better memory footprint seems also to faint as the input file increases in size.
The point the article makes
was quite interesting for me. Searching a little online I found an amazing
ressource for this topic. Given the
#macro implementation of QSORT in qsort.h I measured again.
gcc -o c2 -O2 fileSortNums++.c
/usr/bin/time -v ./c2 rand10000.txt
/usr/bin/time -v ./c2 rand50000.txt
/usr/bin/time -v ./c2 rand100000.txt
/usr/bin/time -v ./c2 rand500000.txt
| Example size | Time [s] C | Time [s] C++ | TimeC/C++ | Memory [MiB] C | Memory [MiB] C++ | Memory C/C++ |
|---|---|---|---|---|---|---|
| 10000 | 0.41 | 1.60 | 0.26 | 1628 | 3428 | 0.47 |
| 50000 | 10.72 | 44.55 | 0.24 | 1752 | 3516 | 0.50 |
| 100000 | 44.58 | 182.56 | 0.24 | 2300 | 3940 | 0.58 |
By replacing the library implementation the weakpoint of C, which was that the compare function that couldn't be inlined into the sort-function is gone.
Conclusion
Given an inlineable sort-function boost the execution speed of C at 4x times the one of C++ for all sizes. The memory advantage stays also now constant at only 50% of what the C++ program uses. Often you hear the argument of C as portable assembler. Seeing it as such it is very nice I think, that the C standard library even gave us a standard sort function and we didn't have to implement it in the first place ;) All in all this was pretty cool for me :D
Nothing against the articels original point, that some features of C++ are out-of-the-box
more sane, then the once of C, some even more efficient. But for me learning C first
gives just different kind of
rewards. Espacially for me as an engineer I learned a lot about the way to work on a low level
with a computer, because you are forced to work with less abstraction.
For sure, the same sort-function could have been used just as well in C++.
Given the educational effect, and that there are enough niches, where it is preferable to know either C or C++ for me it doesn't matter which to learn first or at all. Both languages will give you for sure different levels of insighth, productivity and tools to work with.
For me it was very nice to think about the standart implementations of sort() and qsort(). I hope you learned somethink too from this :)