Your code is slow for several reasons:

1\. You should wrap your code in a proc, otherwise all variables are globals 
and globals are much harder to optimize, in particular they have the same 
lifetime as your program so you can't reuse their memory

2\. `split` in each loop, you are allocating 1 string for the line, then 4 more 
strings for your split and 1 sequence to hold those strings. Memory allocation 
is accompanied with reseting your strings and sequence to binary zero. This is 
a recipe for slowness. Python is faster because its GC reuses already allocated 
memory.

Unfortunately, following the Python code works for quick scripting but is a 
performance pitfall. If you want a fast csv/tsv parser you can use the tips 
from this blogpost 
[https://nim-lang.org/blog/2017/05/25/faster-command-line-tools-in-nim.html](https://nim-lang.org/blog/2017/05/25/faster-command-line-tools-in-nim.html)

I should note that the issue with `split` is true for all languages with 
"vanilla" memory management (C or C++ as well require the same techniques).

This might change in the future if strutils is rewritten with more in-place 
procedures and the `dup` and `collect` macro for the functional high-level API 
without its cost (see v1.2.0 announcement 
[https://nim-lang.org/blog/2020/04/03/version-120-released.html](https://nim-lang.org/blog/2020/04/03/version-120-released.html))

Reply via email to