Tuesday, March 23, 2021

C Is The Best Language



The following text was extracted from my recently revised "C From A To Z" book. The tables and bulleted lists below look far better in that book.

Overview

 The C language has been called: "The only portable assembly language." Which means that C is available on all computers and can be used to write anything from operating systems to device drivers, from web servers to user interfaces. Correctly written C Language programs always run significantly faster than programs written in other languages. This speed and portability has distinct advantages, both economic and application specific:

  • C programs require fewer CPU cores to accomplish the same tasks compared to other languages. 
  • C programs manage their own memory so are not slowed by garbage collection. 
  • C programs are equally adept at handling strings and binary (ISO) data.
  • C programs, even when run by root, can give up special permissions to run more securely.
  • C programs run without the need for an enclosing environment. Ruby, for example, often requires Rails to start before the Ruby program can run. Java, for example, requires its Run Time Environment to be present to run the Java program.
  • C can be used to write operating systems. Unix and Linux, for example were written in C.
  • C can be used to write other languages. C++, Ruby, Python, Java and other languages are written in C.
  • C can be used to interact efficiently with the outside world. Apache is a popular web server that was written in C. Sendmail, postfix and qmail are all written in C and are used for the world-wide exchange of email.
  • C can be used to create databases. Posgres, mysql, and Oracle are all written in C.
  • A well written C program is extremely portable and can be easily built on any system. Sometimes, a built C program can be run on other related systems (such as flavors of Linux) without the need to rebuild it for each system.
C versus other languages
Why should you learn to program in C when there are so called modern languages available? After all, businesses practices often drive language adoption:
  • Many website businesses only hire programmers who can write using the Python, Ruby, Php, or Java languages. 
  • Other website businesses are only interested in object-oriented languages and programmers that use them, such as C++, Python, Ruby, Php, or Java.
So one must wonder, if there is any role for the C language in such a world of specialized languages?
Yes there is! Consider the following:
  • All of these modern languages are written in C: C++ is written in C. Python is written in C. Ruby is written in C. Php is written in C.
  • Programs run 10 to 1000 times faster when written in C than when written in one of these other modern languages.
  • C creates slimmer, more efficient code than these other languages.
  • C programs start up faster than programs written in these other languages.
  • C handles errors more adeptly than do these other languages, which typically use traps to handle errors.

To illustrate, we compare and contrast the speed of C to the speed of those other languages. Note that all of the following tests are available in Appendix B.

C runs faster

The problem with interpreted programs is that they run slower than compiled languages. This lethargy is because the interpreter must read and process the code a line at a time. Ruby, Python, Php, and Perl are interpreted languages. For example the php program, or a library in a web application, must interpret (run) your php code.

Compare the run time speed of these interpreted (script) programs against the run time speed for the C language program. All the programs and scripts did exactly the same thing, count up to ten-million with a loop inside another loop. In the following, the time command produces output in four fields of which we are interested in the third, the total run time:

% time php looptest.php

34.82u 0.07s 0:35.18 99.1%

% time ruby looptest.rb

45.19u 0.06s 0:45.47 99.1%

% time python looptest.py

65.24u 0.09s 1:05.97 99.2%

% time ./looptest

1.58u 0.00s 0:01.60 98.7%

Here, the compiled C program (looptest) ran in 1.6 seconds, whereas the interpreted Php program (looptest.php) ran in 35.18 seconds, which makes the C program 22 times faster. We summarize these results in the following:

Table 1: Interpreted Versus Compiled Loop Test Runtime Comparisons

Language Runtime C Faster By

C Language 0:01.60 1x

Php                 0:35.18 22x

Ruby         0:45.47 28x

Python         1:05.97 41x


Another test that looked at the speed of reading and processing files showed similar results. The same file was opened, read and closed in each step of the loop:

Table 2: Interpreted Versus Compiled Read File Runtime Comparisons

Language Runtime C Faster By

C Language 0:03.90 1x

Php                 2:01.04 31x

Ruby         0:37.60 10x

Python         4:57.01 76x

In the above, the file to read was /usr/dict/words, and its lines were counted 1,000 times.

And finally the comparison of results from a simple test to see if a string contains a valid integer. This test is executed in a loop 10,000,000 times:

Table 3: Interpreted Versus Compiled Integer Test Runtime Comparisons

Language       Runtime    C Faster By

C Language         0:02.00 1x

C++ Language 0:04.01 2x

Php                         0:20.80 10x

Ruby                 2:02.10 61x

The loop included a test to see if the check failed or succeeded.

Clearly the C language runs faster than these other languages by a significant amount. If you want to milk every bit of performance from your hardware, C is the obvious choice.

Consider the scalable cost of hardware versus your fixed cost of labor. Isn't it better to double the performance (code speed) on your existing hardware every six months rather than to double the cost of your hardware every six months? Such amazing efficiencies are only possible with C.