r/AskComputerScience • • May 17 '19

Is it possible to create a language that is faster than the language it is written in?

35 Upvotes

37 comments sorted by

13

u/Bottled_Void May 17 '19

I'm not sure you're asking the right question.

The "language", presuming you're talking a high level language will be converted into a series of machine instructions. The instructions generated and the speed that they run at will be fixed by the hardware.

The conversion into these instructions is performed by a compiler (usually).

So you could ask the question, could you write a C compiler in C that performs faster than the original compiler.

And the answer to that is yes, it's been done many times over.

2

u/enjayay May 17 '19

So the speed of a compiled language is not determined by the speed of language it's written in? The more you learn everyday~

9

u/venuswasaflytrap May 17 '19

It’s kinda like saying can you draw blueprints for a better pen with the pen you currently have. Of course you can.

1

u/enjayay May 18 '19

I like your analogy. So the performance of the language would not depend on the would not depend on that language it is written in?

3

u/venuswasaflytrap May 18 '19

I guess it's worth breaking things down a bit.

  • A language isn't software. A language is a way of expressing instructions in mostly-human readable form. You can write C++ on paper, you don't need a computer.

  • Machine code is a series of instructions for a chip to execute.

  • A compiler is a tool that translates a programming language into machine code.

The performance of any program is going to be dependent on how efficient the machine code is. In a contrived and not realistic example, if I tell you to multiply 1453781*10 together by having a running total and adding 10 to it 1453781 times, then it's going to take a while. You can probably notice that it would likely be faster to add 1453781 to the total 10 times. Or even faster still to just put a zero on the end of 1453781, to get 14537810, because that's what multiplying by 10 is.

A programming language abstracts these details. If you write 1453781*10 in most programming languages, it's up to the compiler to translate that into steps the chip can understand (most chips will have a physical part built in that multiplies numbers, so will send the task to that, but hypothetically the chip might not, and the compiler would have to tell the chip how to multiply numbers using the instructions that the chip can execute).

But in the end once it's machine code, it doesn't really matter where it's from. Yeah you can see some patterns and make assumptions about what produced it, but ultimately if those instructions are efficient doesn't matter what programming language they originally came from.

Interestingly, a compiler is just a set of machine code instructions too. So you could write a compiler in a programming language. Compile it to machine code, and bang you have a new compiler.

And you can make that compiler interpret whatever you want. Maybe you want "56 . 4" to be 56 multiplied by 4 instead of using "*" or "X" or whatever. It's just syntax. You could invent whatever you wanted.

And maybe you could make it more efficient. If you knew more efficient ways for the chip to do things, your new compiler could gain efficiencies that the old compiler couldn't - even though you're creating your new compiler using the old one.

1

u/enjayay May 18 '19

So like a natural language is a way of expressing thoughts & experiences. So a compiler is what turns that human readable code into that machine code.

So what algorithm you use will determine the speed of your program. So some algorithms take less steps than others. So is it all about the least amount of steps you need to take to get from a to b?

So the compiler would turn what you write into a set of instructions/ an algorithm that your computer could execute.

So it does not matter where the machine code is from, it is still machine code and it can be optimized. So some of these patterns that are produced would be executed slower, I presume.

Woah woah woah! Are you talking about compiling a compiler here?

So it really feels to me that knowing how to make the chip do what you want is more important than the language you use. Right? :D

1

u/venuswasaflytrap May 18 '19

Well this is where the idea of “slow” languages come in.

Java, for example, is an interpreted language. So you partially compile java into byte code, and then you install an interpreter on a machine that runs the byte code. The idea is that the byte code works across platforms, because each platform has its own interpreter that can read the bytecode. A side effect of this is that you can’t really access anything super low level just by writing java. The interpreter will handle your code on the chip as it does, and you can’t get in there and optimise it.

The language (interpreter and compiler) also abstracts away lot of the memory management. So it doesn’t let the developer handle details with memory allocation. This is nice because you don’t have to think about it, but it’s another area that you can’t tweak to run faster.

So if you write java in certain ways, it will run slowly, even though it’s not doing anything particularly computationally heavy.

With say, C, it’s much more directly similar to machine code. But you have to do much more low-level tasks like managing memory. So programs written in C are more open to optimisation by the developer. But they’re also more work in a way.

But you could write a compiler in java, that runs in the java interpreter that compiles another source language into machine code that runs directly on the chip.

5

u/deong May 17 '19

A compiler is just a program that converts source code in some language to object code suitable for some computer system. Say you wrote the same compiler in a slow language and a fast one and you give those two compilers the same program to compile. What will vary is the time needed to compile the program with the two compilers. Their output, an executable program, will be the same since you wrote the same compiler twice.

Look at it this way. A skilled person could write hand-optimized machine code that would run really fast compared to the code generated by a compiler. The compiler generated that code in seconds though, and it took the human months or years to write it. The human is a very, very slow "compiler" that produces very fast code. The compiler is extremely fast and produces slower code.

1

u/enjayay May 18 '19

So I could use two implementations of the same compiler, that are written in different languages to produce the same end result, but if one of those compilers were written in a slower language than the other the compile time would probably be slower. Right?

So the lower level the language, the easier it would be to optimize your code?

2

u/deong May 18 '19

So I could use two implementations of the same compiler, that are written in different languages to produce the same end result, but if one of those compilers were written in a slower language than the other the compile time would probably be slower. Right?

Yes, in theory. In practice, attempting to write the same compiler in two different languages will result in subtle differences, as you have slightly different bugs and such, but ignoring that, you're correct.

So the lower level the language, the easier it would be to optimize your code?

That's a little trickier statement to make. Optimization is complicated. You can write low-level code that basically defeats an optimizer and as a result runs more slowly. And minor tweaks to the source code can enable important optimizations. You can make very broad statements like "an algorithm implemented in C will probably run faster than if implemented in Python", but there's no guarantee. In reality, making things faster probably involves profiling your code and looking at what the data tells you.

30

u/CptCap May 17 '19

Depending on your definition of "create a language", yes. If you make a compiler in a very slow, interpreted language (say python), the result will probably be a lot faster than the language it was made in.

24

u/[deleted] May 17 '19

This can be true even within the same language, e.g. using a slow, naive C compiler to write an optimizing C compiler.

-3

u/enjayay May 17 '19

Woah this changes every thing. If you create the same compiler in a fast language like C and in slow language like Python I, the compiler written in C would produce faster executing code right?

0

u/[deleted] May 17 '19

[deleted]

1

u/enjayay May 18 '19

So you can use different languages to get the same result?

0

u/[deleted] May 18 '19

[deleted]

1

u/enjayay May 18 '19

Ahh that is a good one. That really makes it click for me! Thank you so much!

3

u/cbr May 18 '19

Alternatively, you can write the same cake recipe in multiple languages and get exactly the same kind of cake at the end.

3

u/khedoros May 17 '19

The easiest way is to build a compiler for that language into native code, but I suspect that you might be more interested in whether I could build an interpreter in some language, and have things run by the interpreter run faster than things written in the parent language.

For example, could I make a language called Floob, with an interpreter in Python, that runs faster than Python code? I edge toward saying that it might be possible to make it run faster than typical Python code, but not all Python code. Maybe there are convenient parts of the parent language that have a hefty runtime penalty, but the interpreter can be built to avoid those parts.

I've got Asm.js in mind. The input language is typically C or C++, through the Emscripten compiler. The program compiles into Javascript, but a heavily restricted subset of Javascript, that allows certain optimizations to be made, so that a typical asm.js program will run faster than its Javascript equivalent.

1

u/enjayay May 17 '19

Yes I am interested in this. :)

So in short, what you are saying is "If you restrict the child, you could get better performance out of it than you get out of the parent."?

2

u/khedoros May 17 '19

If the parent has a fast (relative to the parent language as a whole) and powerful subset that can be used to implement the child, then I'd guess the answer is "yes, in many cases".

1

u/enjayay May 18 '19

Wow I would have never known this. I completely misunderstood before.

2

u/not-just-yeti May 18 '19

Yes, but probably not too useful in general: If you say "like language X, except recursion is disallowed -- both direct and indirect" -- yeah the resulting language-implementation can probably get rid of the runtime stack (using static memory for each function). But that doesn't mean you want to tell a programmer that to process XML, they need to manage their own stack manually to process the tree-shaped data.

(But on the other hand, for security reasons/bounds you might like restricting a language -- e.g. web-assembly as mentioned, where you want to disallow potentially-abusive scripts, and I think that in the airline flight-control software, no recursion is allowed, nor even loops over large numbers, because they want to prove that a function will always complete within a given time-bound.

3

u/[deleted] May 17 '19

Well yes. Output from a C compiler is optimized in ways most assembler authors would not have predicted. And PyPy is reported to be faster than [C]Python.

1

u/enjayay May 17 '19

How so and really? I would like to know more.

4

u/frikk May 17 '19

PyPy is a freakishly good example of this.

3

u/bacondev May 17 '19

A language in and of itself doesn't have a “speed”. Some languages are prone to be faster than others, but the actual speed almost entirely depends on the implementation. So to answer your question, yes. For example, a C compiler written in Python would generate a faster program than a Python interpreter written in C.

1

u/enjayay May 17 '19

Why would that be the case? This is intriguing.

3

u/bacondev May 18 '19

So machine code is still a language; it's the primitive language that its corresponding CPU understands. If your program is written in language X but gets compiled to machine code, how would the CPU know what the original language was? It can't—at least not without advanced pattern recognition. The resulting machine code could very well have been a compilation of a program written in language Y.

Which compiler is used can make a huge difference. For example, suppose you're analyzing the statement a = b + c. Some compilers might compile that to machine code that includes exactly three instructions. Others might compile it to machine code that includes more than three instructions. The two resulting compiled programs could be significantly different in performance even if the compilers compiled the same exact source code.

Now, considering interpreters, they compile the source at run-time. So this is extra work that can sometimes severely drag out computations.

With some programming languages, when a program that is written in that language is compiled, it is prone to having several unnecessary instructions. For example, since everything in Python is an object, extra instructions must be executed to get the desired result, even if the statement is as simple as a = b + c. There's no guarantee of the data types of a, b, or c, so the interpreter/compiler must account for that via additional instructions.

However, in C, a = b + c already has a lot of information about the values in question, so the compiler can take shortcuts since data types can be assumed to be compatible with the necessary instructions, for example.

1

u/bacondev May 18 '19

So machine code is still a language; it's the primitive language that its corresponding CPU understands. If your program is written in language X but gets compiled to machine code, how would the CPU know what the original language was? It can't—at least not without advanced pattern recognition. The resulting machine code could very well have been a compilation of a program written in language Y.

Which compiler is used can make a huge difference. For example, suppose you're analyzing the statement a = b + c. Some compilers might compile that to machine code that includes exactly three instructions. Others might compile it to machine code that includes more than three instructions. The two resulting compiled programs could be significantly different in performance even if the compilers compiled the same exact source code.

Now, considering interpreters, they compile the source at run-time. So this is extra work that can sometimes severely drag out computations.

With some programming languages, when a program that is written in that language is compiled, it is prone to having several unnecessary instructions. For example, since everything in Python is an object, extra instructions must be executed to get the desired result, even if the statement is as simple as a = b + c. There's no guarantee of the data types of a, b, or c, so the interpreter/compiler must account for that via additional instructions.

However, in C, a = b + c already has a lot of information about the values in question, so the compiler can take shortcuts since data types can be assumed to be compatible with the necessary instructions, for example.

1

u/bacondev May 18 '19

So machine code is still a language; it's the primitive language that its corresponding CPU understands. If your program is written in language X but gets compiled to machine code, how would the CPU know what the original language was? It can't—at least not without advanced pattern recognition. The resulting machine code could very well have been a compilation of a program written in language Y.

Which compiler is used can make a huge difference. For example, suppose you're analyzing the statement a = b + c. Some compilers might compile that to machine code that includes exactly three instructions. Others might compile it to machine code that includes more than three instructions. The two resulting compiled programs could be significantly different in performance even if the compilers compiled the same exact source code.

Now, considering interpreters, they compile the source at run-time. So this is extra work that can sometimes severely drag out computations.

With some programming languages, when a program that is written in that language is compiled, it is prone to having several unnecessary instructions. For example, since everything in Python is an object, extra instructions must be executed to get the desired result, even if the statement is as simple as a = b + c. There's no guarantee of the data types of a, b, or c, so the interpreter/compiler must account for that via additional instructions.

However, in C, a = b + c already has a lot of information about the values in question, so the compiler can take shortcuts since data types can be assumed to be compatible with the necessary instructions, for example.

1

u/enjayay May 18 '19

Do you mean could instead of would?

2

u/soegaard May 17 '19

When thinking about this type of question, it is helpful to draw diagrams using I and T diagrams to represent interpreters and compilers respectively.

A short introduction can be found at page 281 in the chapter "Bootstrapping a Compiler". http://hjemmesider.diku.dk/~torbenm/Basics/basics_lulu2.pdf

1

u/enjayay May 17 '19

I will look up that diagram. Thanks so much for the link! I am so into the idea of bootstrapping and I really love diagrams.

2

u/tbrownaw May 18 '19

Aside from the compilers and restricted subsets already mentioned, there's also the question of "faster given what?". What costs and constraints are you working under?

Lots of heavy math and data science code uses Python. Pure python code is "slower" than C, but all the heavy lifting is done by a (fast, compiled) library. So it's not actually (much) slower.

But! Because you have that library, and because Python is much higher-level, you have much less busy-work to do. Which means that given a set amount of development time, you can spend more time on writing better algorithms. So your code will end up "faster" than what you would have written in C, even tho it's still slower than an identical program translated into C.

Modern CPUs have their public instruction set, but operate internally using microcode. If you could write programs directly in microcode, they'd actually be slower than assembly, because assembly code is more compressed and the limiting resource is (usually) memory bandwidth.

Similarly, a tiny langue that (1) has an interpreter that fits into L1 cache, and (2) is more compact than assembly, will probably be faster unless the code has a lot of tight loops.

Aside from these (well, briefly touched on in the first one), you can cheat by redefining what "faster" means. If something executes a bit slower but is much faster to develop in, the overall result might still be less time. And will definitely be less developer time, which is more expensive than machine time.

Say I have two programs that I need to write and run and get results from. If I write them in a "fast" language, each takes 6 hours to write and 2 hours to run. It takes me two days. Or I can write them in a "slow" language, where I need 3 hours to write each of them but they take 10 hours to run. So I write them both in one day, and run them on separate cloud instances overnight.

1

u/enjayay May 18 '19

Ahh so because a language like Python would allow you to do more in less time, you would have more time to write better algorithms. Is this what you are saying?

So you are talking about tradeoffs. So in a low level language it might take me longer to write a program, but less time for the machine to execute it, however if I write a program in a higher level language it might take less time to write, but more time to be executed by the machine.

2

u/tbrownaw May 18 '19

Ahh so because a language like Python would allow you to do more in less time, you would have more time to write better algorithms. Is this what you are saying?

Yes, exactly. :)

So you are talking about tradeoffs.

Yep, that's the other half.

2

u/SYStem_OP May 18 '19

In a formal language design perspective, "faster language" may mean a language that provides same semantics with a less number of reductions or operations. In assembly language, for example, adding two numbers involve 2 memory read, 1 register addition, and 1 memory write, but in high-level language like python, it can be computed by 1 operation.

However as may others mentioned, executing program needs compilation or interpretation of the programming language, which in practice will be translated into machine language. The actual 'speed' of a execution will depend on optimization and execution sequence of machine operations.

1

u/enjayay May 18 '19

So in short how well the machine code is implemented is what will determine the execution speed. Correct?

15

u/icendoan May 17 '19

Let's rephrase the question: is it possible to write an arbitrarily slow compiler? And the answer is yes.

2

u/roman_fyseek May 17 '19

The word you're looking for is 'compiler'.