r/AskComputerScience • u/enjayay • May 17 '19
Is it possible to create a language that is faster than the language it is written in?
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
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
May 17 '19
[deleted]
1
u/enjayay May 18 '19
So you can use different languages to get the same result?
0
May 18 '19
[deleted]
1
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
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
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
4
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 ofa,b, orc, so the interpreter/compiler must account for that via additional instructions.However, in C,
a = b + calready 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 ofa,b, orc, so the interpreter/compiler must account for that via additional instructions.However, in C,
a = b + calready 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 ofa,b, orc, so the interpreter/compiler must account for that via additional instructions.However, in C,
a = b + calready 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
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
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.