eli5: How is C still the fastest mainstream language?

835 views

I’ve heard that lots of languages come close, but how has a faster language not been created for over 50 years?

Excluding assembly.

In: 2072

19 Answers

Anonymous 0 Comments

Because 99.99% of the time you don’t care about how fast something is (which can be improved by a programming language change). It’ll be fast enough most of the time and you save so much time using a modern programming language compared to C. Sure there’s times where things need to be fast, and that’s when you use C

Anonymous 0 Comments

C was written in an era of efficiency, where every byte mattered, to be close to the way the machine operates, to use direct memory manipulation (which can be a security issue if not managed, because it doesn’t have lots of tests and checks to make sure you don’t do dumb stuff), and to be merely an interface between people who had been programming in assembly up until then so that they could write in a slightly higher-level language but still retain close-to-metal performance.

It had to run on all kinds of machines, be very efficient, and not “do too much” that the programmer wasn’t aware of.

And you can write an almost-complete C99 compiler in only 76,936 lines of code. That’s tiny.

C is nothing more than a shim of a language sitting on top of assembly, with some useful functions, that’s evolved into a fuller language only relatively recently.

Anonymous 0 Comments

There are 2 major factors.

First is that C is about as close as you can get to assembly without writing assembly. The compiled binary is basically assembly. What you write is for the most part directly translated into assembly. You can’t really get “faster” when you don’t add anything on top of the fastest thing around. Other modern languages add bells and whistles that will add overhead to the final program. Probably one of the common ones is garbage collection.

The second is compiler level optimizations. C, because of how long it has been around, has compilers that are incredibly good at optimization. Companies like Intel and AMD have large teams dedicated to the C compiler. This is incredibly complicated and even small changes can have massive impacts on the performance of the final program. These optimizations will often transform the logic you wrote in C into a series of assembly instructions that can be very convoluted to understand. But are necessary for performance be it for the purposes of speculative code execution or L1/L2 caching or something else.

Anonymous 0 Comments

C is a fairly unsafe language. If I allocate 20 bytes of memory and then write to the 21st byte, C will let me do that no questions asked and my program may or may not crash. Do you feel lucky?

Most languages have traded raw speed for varying degrees of safety to allow programmers a better chance of writing correct bug free code. The safety and the abstractions cost a little bit of speed.

Further, some languages have even more constraints such as the ability to run a program on any hardware (Java and some others), this is more costly still.

Anonymous 0 Comments

C was designed to be efficient by providing direct access to memory via address pointers. It also has the benefit of having a large coding base and decades of support that have allowed the developers to improve the efficiency by making use of individual target machines’ assembly language enhanced instructions.

On the downside, C accomplishes its speed by not automatically adding typical bounds checking (arithmetic overflows, array overruns, pointer validity) that are often built into other languages.

Anonymous 0 Comments

There are a few ways to look at coding. One is from an electrical engineer centered view and the other is from a mathematical centered view. C comes from designing software with the hardware in mind. So it doesn’t stray far from the underlying details. That keeps if fast. As long as your language doesn’t take you too far from the hardware, its going to be fast. But creating a new language syntax without solving any problems gets you nowhere.

But there are many modern programmers who want to solve problems without really caring how the underlying hardware works. So extra layers are added to the languages to allow for this. But layers slow things down. But for many cases it doesn’t matter.

Modern languages get fine tuned all the time to get the best of both worlds but you can’t escape the fact that the closer to machine instructions your are the faster your program runs.

Anonymous 0 Comments

C code is really close to what the CPU is doing, plus there are no validity checks on what you are doing. Use a string as a memory pointer? No problem. Have a function return a pointer to another function but use it as an integer? No problem.

A long time ago I explained to a friend that C doesn’t just let you shoot yourself in the foot, it loads the gun, loosens the trigger, cocks the gun, puts it in your hand pointed at your foot, then shouts “BANG!”

Anonymous 0 Comments

The correct phrase is like “it is possible to implement language A to be faster with some programs than language B on the given hardware”, not “language A is faster than language B”.

Anyway, that’s not entirely true. Fortran is faster than C. The reason is that in C there is a **restrict** keyword, which is rarely used, while in Fortran all libraries are “restrict” by default. Also the typical implementation of printf and other functions with variable number of arguments are slow. Fortran avoids this by making i/o part of the language itself, not standard library.

However, Fortran can be 1% faster, but is more difficult for a programmer.

Anonymous 0 Comments

What’s assembly? (This is eli5)

Anonymous 0 Comments

Others have the majority of the answer covered, and there’s one additional angle that I think is important to highlight – C isn’t _inherently_ fast, it just has language features that make it well-suited for writing computationally efficient code and essentially enable you to trade development efficiency for execution efficiency. It’s not very hard to find/create examples of well-written code in other languages that is faster at a particular task than average or poorly-written C code, especially if the other language is one that was designed specifically for working on that kind of task.