💾 Archived View for avfig.com › gemlog › raytracing-vs.gmi captured on 2022-03-01 at 15:19:14. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Ray Tracing in Common Lisp vs. C++

By Jeremiah Stoddard on January 9, 2022

I mentioned a couple of posts ago that I worked through the short book "Ray Tracing in One Weekend" and did the exercises in Common Lisp over Christmas weekend. I was pleased with the results, but a little concerned about the performance; I know raytracing doesn't have a reputation for being a speedy process (though I understand that with cutting edge GPU technology, it is starting to make an appearance in the latest games), but I was worried that my code was much slower than it needed to be. Some improvements were made based on a suggestion from a helpful person in #lisp on libera.chat, but a 600x400 pixel image still took well over an hour to generate when performing 100 samples per pixel. I don't like lowering it because the resulting image is not great at 10 samples per pixel.

Last night I decided to install g++ and cmake on my computer in order to compare the speed between my Common Lisp version of "Ray Tracing in One Weekend" and the original C++ version. In order to keep the process within the limits of my patience, I set both versions to generate a 16x9 version of the final scene at 300 pixels wide and 10 samples per pixel. Here are the results for the C++ version, compiled with g++ 10:

$ time ./inOneWeekend >image.ppm
Scanlines remaining: 0   
Done.

real	0m16.873s
user	0m16.868s
sys	0m0.004s

And for Common Lisp, running in SBCL 2.1.1.debian (straight from the Debian repository):


Scanlines remaining: 0     
Done.
Evaluation took:
  140.909 seconds of real time
  141.579764 seconds of total run time (139.513155 user, 2.066609 system)
  [ Run times consist of 12.929 seconds GC time, and 128.651 seconds non-GC time. ]
  100.48% CPU
  394,997,464,716 processor cycles
  382,928,307,712 bytes consed
  
NIL

As can be seen, the C++ version was only about 8 times faster than my Common Lisp version. I am sure an expert Lisper could get better performance, but I really can't complain about the result: It is within an order of magnitude of the language known for speed. Assuming Common Lisp (as used by me) performs this well comparatively in other tasks, it will certainly be sufficient for my needs, and particularly for the small, lightweight games I would like to make.

For anyone interested, my Common Lisp version of the "Ray Tracing in One Weekend" source code is available on Github at:

Common Lisp Implementation of Ray Tracing in One Weekend

Back to Jeremiah's Gemlog