Ray Tracing Next Week in Rust and C#

I ported the implementation of the Ray Tracing the Next Week book to C# and rust, as I was keen to revisit learning rust with a more interesting project. I also had a look at performance between the two implementations.

I was fascinated in 3D rendering at a young age, using Lightwave3D on the Amiga, and then POV-Ray. The Ray Tracing in a Weekend series of books is a great introduction to ray tracing, and the end of the next week produces this impressive image:

/blog/2026-01-31-ray-tracing-next-week-in-rust-and-csharp/images/highqual.png

The C# port is here: https://github.com/taumuon/RayTracingNextWeekCS and the rust port here: https://github.com/taumuon/RayTracingNextWeekRust

Both ports closely follow the original C++ code. The C# code is parallelised with the TPL, and the rust with Rayon. I didn’t try to optimise the C# code much, other than using structs as appropriate.

In the rust code, the hittable and material interfaces are implemented as trait objects. Some of the objects in the scene share materials, so the scene objects hold Arc<dyn Material>. This is similar to the c++ implementation which takes shared_ptr.

The scenes are unchanging after creation, the hittables and materials used in the scene are guaranteed to stay alive for the entire time the scene is being rendered. I created a second rust implementation to avoid the overhead of accessing and cloning Arcs, by passing around an index into an vec.

I ran the code on a laptop with an AMD Ryzen 7 4800H cpu, and 16GB memory.

The numbers below are the times in milliseconds to render a 300x300 pixel image, with 80 samples per pixel (the high resolution image above is a 600x600 pixel render with 200 samples per pixel).

The dotnet compiler version was 10.0.101, and rust. 1.91.1

Run C# Rust Rust 2
1 8915 8833 6428
2 8872 8205 6781
3 9511 8520 6747
4 9287 8912 6885
5 9729 8760 6877
Avg 9263 8646 6744

It seems that the c# code is approx 7% slower than the first rust implementation, which is nearly 30% slower than the more optimised rust implementation. I’m a little hesitant to say for definite that rust is faster than c#, as there’s a chance of there being bugs or slight differences in the implementation.