3 ms·
> What, then, is a good way to compare languages? Digging into details and yielding very specific conclusions. Let’s take the example benchmarks. The first o
by espadrine 6y ago
> What, then, is a good way to compare languages?
Digging into details and yielding very specific conclusions.
Let’s take the example benchmarks.
The first one, Fibonacci, can only be properly assessed by inspecting the assembly. It is an analysis of compiler output, not of the language, not even of compiler optimizations.
It therefore only measures the CPU speed of function calls, and one compiler outputs a different set of assembly instructions:
• Go (go tool objdump fib) is all like “CMPQ JBE(label(morestack)) … LEAL(-1,cx) MOVL(cx) CALL(fib) …×3 ADDL(-2,cx) MOVL(cx) CALL(fib) MOVL ADDL …×3 RET label(morestack) CALL(morestack) JMP”.
• Crystal (crystal build --emit=asm fib.cr) is all like “subl(1) …×6 callq(fib) …×1 subl(2) …×7 callq(fib) movl addl …×6 addq retq” for the recursive condition.
So the conclusion is that Go outputs less instructions, but has to maintain its segmented stacks (useful for goroutines, its lightweight threading system) on every call, while Crystal uses the standard assembly calling system with little overhead beyond light dynamic typechecks.
So there is a language-motivated difference: calls in Go are a tiny bit more expensive, but built-in lightweight threads consume little memory because their stack can increase dynamically according to use[0].
The second benchmark, HTTP servers, has everything to do with the implementation of the default library, and nothing to do with either the language or the compiler. And I presume Crystal is backed by an optimized C library for TCP[1] while Go is probably a full reimplementation of TCP and HTTP.
[0]: https://blog.cloudflare.com/how-stacks-are-handled-in-go/ https://blog.cloudflare.com/how-stacks-are-handled-in-go/
[1]: https://github.com/crystal-lang/crystal/blob/master/src/socket/tcp_socket.cr https://github.com/crystal-lang/crystal/blob/master/src/sock...
- abainbridge 6y ago> Go outputs less instructions, but has to maintain its segmented stacks I think they dropped segmented stacks in 2014: https://en.wikipedia.org/wiki/Go_(programming_language)#Version_history https://en.wikipedia.org/wiki/Go_(programming_language)#Vers... The stack can still grow, but contiguously. I'm not sure if there are any instructions required to monitor the stack size and grow if it needed.
- amscanne 6y ago> CMPQ JBE(label(morestack)) … ^^ It's still the preamble for most functions :)
- filleduchaos 6y ago> And I presume Crystal is backed by an optimized C library for TCP[1] On what basis are you presuming that?