Smallpt: Global Illumination in Swift
Swift C++
Switch branches/tags
Nothing to show
Clone or download
jackpal Remove unneeded collect queue.
I added it on speculation that Swift arrays were not threadsafe for
independent element access. But it turned out that the crashes I
was seeing were caused by recursion overflowing thread stacks.
Latest commit 709c286 Oct 27, 2014

Small Path Tracer in Swift

This is a straightforward translation of to Swift.

Example Image

An example output image


  • Install Xcode 6.1
  • Install a PPM image viewing program such as Toy Viewer

Build and run

xcrun swiftc -O -sdk `xcrun --show-sdk-path --sdk macosx` smallpt.swift
open image.ppm

You can also run the swift file directly. And in either case you can supply the number of samples per pixel. (Default = 4 samples per pixel.):

./smallpt.swift 512 && open image.ppm


On a MacBook Pro (Retina, 15-inch, Early 2013):

Language time (seconds)
C++ single threaded 5.6 s
Swift single threaded 13.8 s (of which 5.6 s is rendering, 8 s is file output)
Swift GCD 9.8 s (of which 1 s is rendering, 8 s is file output)


  • Swift file output seems to be very slow compared to C.
    • partly because it is unbuffered
    • partly because it is converting from Unicode to UTF8