-2

I found that the initial compiler for Rust language was written in Ocaml. So what i thought was that Rust would be similar to Ocaml performance wise. But when I look at benchmarks Ocaml vs C++ and Rust vs C++ and compare Ocaml with Rust. It gives me what...?, but how...?

How can Rust be more faster in performance compared to Ocaml even though it derived from Ocaml?

and then it gives rise to an another question ???

Can a compiler based language derived from C be faster than C itself performance wise?

ruohola
  • 21,987
  • 6
  • 62
  • 97
kanudo
  • 2,119
  • 1
  • 17
  • 33
  • 5
    It is not like final executable was running on top of Ocaml. You can write C compiler in Java for example but it won't effect output. – user7860670 May 25 '19 at 12:16
  • 2
    Having a compiler in Ocaml (or anything else) for Rust does not mean Rust is derived from Ocaml. You could write a C++ compiler in Visual Basic if you felt like it. – Mat May 25 '19 at 12:18
  • 1
    I suggest you read up on compiler bootstrapping, it's a fascinating topic: https://en.wikipedia.org/wiki/Bootstrapping_(compilers) https://stackoverflow.com/questions/193560/writing-a-compiler-in-its-own-language – ruohola May 25 '19 at 12:18
  • @VTT means even if the C compiler gets written in Java, the executable compiled from this C compiler can be way faster than executable compiled from Java itself? or the executable compiled from this java based C compiler can be equal in performance with an executable compiled from compiler like Clang or GCC – kanudo May 25 '19 at 12:23
  • 2
    I mean there is no direct dependency between language compiler is written in and the performance of the compiled code. It is not even clear why would you assume that such a dependency exists. You know, some people can draw colored pictures in text editor even tough it is not the fastest way. – user7860670 May 25 '19 at 12:27
  • @kanudo, Yeah the language the compiler is written on just affects the compilers speed and thus the compile time. It has no effect on the speed of the compiled program. – ruohola May 25 '19 at 12:35
  • 7
    When I use a truck to transport the parts of a Ferrari, and then have humans assemble those parts, that doesn't mean the Ferrari isn't going to be faster than a truck or a human :-) – Nikos C. May 25 '19 at 12:37
  • @VTT then am i getting it right that, machine code is completely independent of the compiler or the programming language used. – kanudo May 25 '19 at 12:46
  • 1
    @NikosC. it's like saying "If humans make computers, how can computers be faster than people?" :-) – Aykhan Hagverdili May 25 '19 at 12:59

2 Answers2

14

The compiler simply generates the (machine) code that is going to be run. The resulting program doesn't run on top of the language its compiler was written in. Hence, there is no correlation between performance of a particular implementation and the language it was implemented in.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • Means machine code is independent of compiler or the programming language used? – kanudo May 25 '19 at 12:43
  • 1
    @kanudo Yes it is. It is the language your CPU understands and can directly execute – Aykhan Hagverdili May 25 '19 at 12:44
  • Yes. But a c++ compiler may emit different instructions than the rust compiler and both may still do the same thing @kanudo – Hemil May 25 '19 at 12:45
  • So the answer is that machine instructions generated by Ocaml compiler are slower then the machine instructions generated by Rust compiler and hence executable from Rust does same thing with speed in which executable from Ocaml takes more time. Did i get it right? And so that is the outcome of a lanugage design and not in which the language was implemented. – kanudo May 25 '19 at 12:50
  • @kanudo not exactly. The machine code runs at the same speed. Ocaml may be doing more things like checks and stuff or may be using less efficient algorithms or something like that. – Aykhan Hagverdili May 25 '19 at 12:54
  • 1
    @Ayxan: Specifically, isn't OCaml garbage collected? – Matthieu M. May 25 '19 at 18:05
  • 2
    @MatthieuM. According to their [website](https://ocaml.org/learn/tutorials/garbage_collection.html) "... OCaml provides a garbage collector so that you don't need to explicitly allocate and free memory as in C/C++." That too probably contributes the reason why its performance falls behind Rust and C++ – Aykhan Hagverdili May 25 '19 at 18:27
2

The language the compiler is written on just affects the speed of the compiler and thus the compile time. It has no effect on the speed of the compiled program, since that depends only on the generated machine code.

In principle you can write a C++ compiler with brainfuck, run the compiler in some really slow brainfuck interpreter and have the output be the most sophisticated and efficient machine code ever. And thus your final program will be lightning fast.

I suggest you read up on compiler bootstrapping, it's a fascinating topic:

en.wikipedia.org/wiki/Bootstrapping_(compilers)
https://stackoverflow.com/questions/193560/writing-a-compiler-in-its-own-language

ruohola
  • 21,987
  • 6
  • 62
  • 97