Hacker News new | past | comments | ask | show | jobs | submit login
Comparing the Same Project in Rust, Haskell, C++, Python, Scala and OCaml (2019) (thume.ca)
52 points by bubblehack3r on Feb 6, 2023 | hide | past | favorite | 7 comments



This is such a good analysis and case study that I almost don't want to summarize it and let folks read it, but I know not everyone will and miss out. I also want to list things out for my own sake and future reference. Read the full article though instead of reading 5 other short ones.

Rust (baseline) 1.0x -- smaller using hand-written lexer and recursive descent parsing, mutation using visitor

Haskell 1.3x -- NFA -> DFA lexer generator, LR parser -> AST, full tree rewrites

C++ 1.4x -- LR parser and tree rewriter, lack of sum types and pattern matching, header files (subjectively estimated compile times similar to Rust baseline)

Python 0.7x -- most extra features, LR parser, dynamic typing just set new fields on AST, introspect fields via __dict__, metaprogramming with eval code snippits (maybe ~2x expressive as baseline)

Rust (another team) 3x -- DFA lexer and LALR(1) parser, fully typed parse tree instead string-based, TryFrom vs Result, boolean field/constraints and return type multiplication factor, data structure for assembly instructions

Scala 0.7x -- use provided cmdline LR table generator, Python script translated scraped Java grammar from web page. (compared to Rust baseline somewhat more expressive with more syntactic sugar and no borrow-checker to make happy)

OCaml 1.15x -- LR parser generator and tree rewriting for parsing, as well as a regex->NFA->DFA conversions for lexing. (Rust and OCaml seem similarly expressive except that OCaml needs interface files and Rust doesn’t.)

The above is a barely meaningful summary but it does get across the idea that there isn't all that much difference in expressiveness in the hands of similar and very skilled authors, except for the Python case which sacrifices static-typing safety and performance as well as using metaprogramming requiring more knowledgeable/skilled maintenance.


typo: Python was 0.5x instead of 0.7x


Personally, I'm missing a more in depth write up. My takeaway has been "it doesnt matter because everyone had a different approach", not talking about the languages much beyond noting that one needs more declarations than the other. Nonetheless, this is still an interesting post. It does make the feeling of missing out come back in me, though, thinking about all the group work I've done during my bachelor's degree: No one is invested enough to make a plan or even attempt to write something resembling anything close to "clean code". But it's nice to know this isn't the norm!


Dataset is not large enough to draw any conclusions. But execution time would've been an interesting metric to include.


> Fancy metaprogramming can also make it more difficult to understand how code works.

Metaprogramming looks very exotic and unusual on the surface, but many languages do very similar things in forms of macros and code generation. However, only the final, actually compiled code is usually counted in analyses like this one. Being fully dynamic, Python allows for such "preprocessing" of the code itself to happen at runtime, which I find much more powerful.


Discussed at the time:

Comparing the Same Project in Rust, Haskell, C++, Python, Scala and OCaml - https://news.ycombinator.com/item?id=20192645 - June 2019 (370 comments)


OCaml doesn’t need interface files.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: