eli5: How is C still the fastest mainstream language?

748 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

Imagine I ask “How long is the fourth Lord of the Rings film?” Most modern languages would just refuse to answer and say “there is no fourth LotR film”, whereas a C program would blindly try to look up the fourth LotR film, find Lost in Translation instead, and happily answer “102 minutes”.

C will, by and large, assume you know what you’re doing and do exactly what you tell it to, and it’ll assume that any weird corner case you haven’t bothered checking for can’t happen. Having this level of control is great for performance, but _having to take_ that level of control is terrible for safety and security.

Inversely, most modern languages have a bunch of automatic checks in place for a lot of those corner cases (like the “trying to read past the end of a list” problem I alluded to with the DVDs). Fundamentally, there’s no free lunch here, and performing those checks means your program is doing more work. Doing more work is always slower than not doing it, so a language that forces those checks can never be as fast as a language that doesn’t.

Because those modern languages were created with the benefit of hindsight, we know that safety and security matter, and that those checks are quite literally the difference between a program crashing when it detects a problem, or letting a hacker read all your data because it missed the problem. We know that programmers aren’t superheroes, and we make mistakes, and we overlook some of those weird exceptional cases, and we all around need as much help as we can get if we’re to write complex software with as few nasty bugs as possible. So modern languages are _deliberately_ slower than C, because we’ve collectively agreed that the benefit justifies the cost.

Also, it’s easy to forget that C is incredibly fast precisely _because_ it’s been around for decades. Mainstream C compilers are incredibly mature, and there’s centuries of work-hours of research and development poured into making those compilers generate better code. Newer languages that don’t build on that work just have a lot of catching up to do.

You are viewing 1 out of 19 answers, click here to view all answers.