As i'm trying to learn Nim i'm attempt at implementing Card Raytracer from https://github.com/Mark-Kovalyov/CardRaytracerBenchmark . Here is a my solution - https://gist.github.com/harungo/5ac94ecd04c7fa608d097da3ed2f0207 Using this compiler's options:
nim cpp -d:release --gc:markAndSweep
the execution time is around 55 seconds on my computer. Maybe someone could take a look at my code and pointed out weak points and what i've did bad or wrong.
You are using many procs like
proc sum(l: Vector, r: Vector): Vector =
Vector(x: l.x + r.x, y: l.y + r.y, z: l.z + r.z)
Of course that functional style can be very expensive. You may try marking that proc with {.inline.} pragma or compile your program with gcc option -flto or both.
The fact that your Vector is a reference to object is not really helpful for performance of course, that is one level of indirection, and each call to a proc like sum() is a new heap allocation.
Maybe the compilers can optimize away all that overhead -- I don't think so.
There may exist much more to optimize in your code, I have not really looked at it, but these points are obviously.