September 14, 2019

C/C++ was designed for number crunching


In the early 1990s, the C++ programming language was already known. In that time, it was described as a complicated but powerful programming language. Surprisingly, C++ was never the only programming language under the UNIX operating system. A well known handy to use example was the PERL scripting language which is working the opposite over C++. The controversy between C++ and Perl is very similar to today's arguments which are speaking for or against Python. The interesting point in the early 1990s was, that Perl didn't replaced C++ and the other way around, but both languages were used in parallel. It was the beginning of a language variety, in which new languages were developed quickly.
The reason why scripting languages like Perl, PHP, Python and Javascript were developed is obvious, because they are focussed on the speed of program creation but not on the performance of the resulting code. The advantage of C/C++ is the other way around. The language is used heavily in the domain of number crunching, in which the CPU runs with 100% and it's important to make the program faster. A typical example for a C/C++ application would be a video encoder.
The interesting point is, that C/C++ is well suited for such applications. The programmer has to invest more time until the program is created, because he has to deal with pointers, variables types and complicated compller workflows, but in exchange he gets a software which outperforms Perl and Python easily. Encoding a video with Python would take 10 hours, but the same task can be done with C++ in under 10 minutes.
Outside of number crunching C/C++ has nothing to offer. If the program doesn't occupies the CPU by 100% which is true for a normal desktop application or a simple game, C/C++ has no advantage over a scripting language. The programmer has to deal with the complex language syntax and has to realize the program with pointers but he gets nothing in exchange. That is the reason, why most programmers stay away from C++ and preferring scripting languages.
Not the programmer but the CPU has to decide, if C/C++ make sense. If the application takes 100% of the CPU, it make sense to rewrite the existing Python code with C/C++. The result is an improvement in performance. Which means, that a compiled C/C++ program is using the existing CPU more efficient. The generated assembly statements are highly optimized and this allows the CPU to run more efficient. How many programs take 100% cpu time? Only a few, in a typical programming situation it's the exception, that the programmer has created an algorithm or a subroutine which occupies all the ressources. Dedicated number crunching problems are not very common in normal programming tasks. The typical webframework, the normal GUI application and even the average 3d game doesn't use the CPU intensively. Switching to the C/C++ language is not needed here.
The problem is, that some programmers understand software engineering and number crunching application as the same. They identify themself with a social role, in which computers are used mainly for important number crunching applications. The idea is, that the computer is started in the batch mode, has to add millions of numbers and then he prints out the result to the screen. And the programmer is trying to realize such software with C/C++. The problem is, that this application is not there in modern software engineering. Today's software is at foremost complex, which means, it contains of thousands of sourcecode lines and is described in hundred of pages of documentation. At the same time, this software need only a little amount of CPU ressources. That means, after the software is started, the CPU demand has increased only from 2% to 3%. If the software makes trouble, it's not because the computer is too slow, but because there is a bug in the software, which throws an exception.
Perl
The invention of Perl has lead into many newly written sourcecode. Instead of propagating code reuse, the Perl community has invented everything from scratch. Today, all the written code is useless, and most of them was rewritten in modern languages like Python. At the same time, the Perl language was loved by it's users, because it was easier to master than C/C++.
From a historical perspective, Perl was the first important scripting language. It was invented by the advent of Java or Python. The concept behind Perl was very similar to what Python is today, because the programmer is no longer forced to declare variables before it's first usage and he doesn't need to compile the program into machine code. In contrast, to C/C++ Perl is a dynamic scripting language. It's main purpose is to create software prototypes. Sometimes, these prototypes are rewritten in C/C++ later, but in most cases the Perl code is used on a production server. This kind of strategy was replicated by modern programmers who are running their PHP and Java applications on a production server.
The reason why Perl and other scripting language have became successful is because the computer speed was fast enough at a certain time. If the created console application doesn't need much cpu power but waits most of the time for a server request, there is no need to optimize the application in terms of performance. Let me give an example. The same is true for cron jobs and GUI applications. In 99% of the running time the app is in the idle mode, that means, it does nothing but waiting for a user input. Does it make sense to optimize the software in terms of low cpu consumption? It's a rhetorical question and explains, why C/C++ has fallen out of fashion.
From the point of view of a C/C++ programmer the CPU is occupied by the current application. That means, the software produces a heavy load in term of memory consumption, cache requests and integer multiplication. And the question is, how to program the software in a way, that it will run with less energy. That means, how to optimize the assembly instructions and the memory map so that the CPU works smooth.