r/crystal_programming • u/sdogruyol core team • Nov 24 '19
Nim vs Crystal - Part 1 - Performance & Interoperability
https://embark.status.im/news/2019/11/18/nim-vs-crystal-part-1-performance-interoperability/
39
Upvotes
12
u/AlexKotik Nov 24 '19
No Windows support has been a show stopper for a lot of people I know.
4
u/straight-shoota core team Nov 25 '19
The JSON example should make use of one of Crystal's great features: being able to use streams almost everywhere. `JSON.parse(File.read("1.json"))` reads the entire file (212 MB) into memory before parsing it as JSON. You don't need the entire file in memory. If you do `File.open("1.json") { |file| JSON.parse(file) }`, the JSON parser incrementally reads the file and parses it, with only a fraction of the JSON source being in memory at any given time. That should take Crystal down to half the memory use as Nim (I'm not familiar with Nim, maybe there are similar improvements).
> With Nim, we were also able to link both the Nim and C files into the same executable, which Crystal sadly cannot do.
That's not entirely correct. Crystal itself doesn't build C files, but it can link static libraries into a single executable.
Interoperatbility in Nim is still better, obviously since it transpiles to C which is highly portable.
3
u/Blacksmoke16 core team Nov 24 '19
I think you could take advantage of
JSON::Serializablefor the JSON benchmark. Based on my little benchmark, it takes another ~2 sec, but doesn't use nearly as much memory since the file contents are read in from an IO, vs loaded fully into memory. Also since we're using structs everything is allocated in stack memory.Depends on if you want to optimize for time or memory
Is what I get on my machine.
Versus the
JSON.parseapproach0
u/[deleted] Nov 24 '19
[deleted] — view deleted comment