Skip to content

Language Shootout

A user-supported site Fastest, Shortest, Simplest

Mandelbrot

Swift #1 — Mandelbrot

Plot the Mandelbrot set on an N-by-N bitmap and write it out in portable bitmap format.

Time 14,921.4 ms
CPU time 14,912.8 ms
Peak memory 8,868 KB
gz 408 bytes — comments removed, gzipped
Style ★★★☆☆
Implementation Swift — Swift 6.1.2
By sysop-
Submitted September 24, 2026

Style assessment

A faithful C transliteration: snake_case names (bit_num, byte_acc) and capitalized locals (Zr, Cr) clash with Swift's lowerCamelCase convention, and output goes through Glibc's putc rather than Swift I/O, though that C interop is an accepted benchmarks-game speed technique. The logic itself is simple and readable; camelCase names, a UInt8 accumulator, buffered writes via Data/FileHandle, and a row-computing function would raise it.

// Preferred filename: main.swift -- swiftc only permits top-level code in main.swift.
/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   direct transliteration of Greg Buchholz's C program
   contributed by Isaac Gouy, fix by David Turnbull
*/

import Glibc

let w: Int = Int(CommandLine.arguments[1])!
let h = w

var bit_num = 0, i = 0, byte_acc: Int32 = 0
let iter = 50, limit = 2.0
var Zr, Zi, Cr, Ci, Tr, Ti: Double

print("P4\n\(w) \(h)")

for y in 0..<h {
   for x in 0..<w {

      Zr = 0.0; Zi = 0.0; Tr = 0.0; Ti = 0.0
      Cr = 2.0*Double(x)/Double(w) - 1.5; 
      Ci = 2.0*Double(y)/Double(h) - 1.0

      i = 0
      while i < iter && (Tr+Ti <= limit*limit) {
         i += 1
         Zi = 2.0*Zr*Zi + Ci
         Zr = Tr - Ti + Cr
         Tr = Zr * Zr
         Ti = Zi * Zi
      }

      byte_acc <<= 1
      if Tr+Ti <= limit*limit { byte_acc |= 0x01 }

      bit_num += 1

      if bit_num == 8 {
         putc(byte_acc,stdout) // Glibc
         byte_acc = 0
         bit_num = 0
      }
      else if x == w-1 {
         byte_acc <<= (8-w%8)
         putc(byte_acc,stdout) // Glibc
         byte_acc = 0
         bit_num = 0
      }
   }
}

Back to Mandelbrot