The Computer Language
Benchmarks Game

Swift versus Java fastest programs

Back in April 2010, Russ Cox charitably suggested that only fannkuch-redux, fasta, k-nucleotide, mandlebrot, nbody, reverse-complement and spectral-norm were close to fair comparisons. As someone who implemented programming languages, his interest was "measuring the quality of the generated code when both compilers are presented with what amounts to the same program."

Differences in approach - to memory management, parallel programming, regex, arbitrary precision arithmetic, implementation technique - don't fit in that kind-of fair comparison — but we still have to deal with them.

These are only the fastest programs. There may be additional measurements for programs which seem more-like a fair comparison to you. Always look at the source code.

mandelbrot
source secs mem gz busy cpu load
Swift 3.19 39,268 1138 12.66 99% 99% 99% 100%
Java 6.90 78,956 796 27.14 99% 98% 98% 98%
pidigits
source secs mem gz busy cpu load
Swift 1.75 8,880 600 1.83 1% 2% 3% 99%
Java 3.07 39,184 938 3.29 99% 3% 4% 1%
fannkuch-redux
source secs mem gz busy cpu load
Swift 10.24 7,484 1097 40.48 100% 100% 98% 98%
Java 17.93 34,616 1282 70.89 98% 100% 98% 99%
spectral-norm
source secs mem gz busy cpu load
Swift 3.96 7,972 601 15.79 100% 100% 100% 100%
Java 4.20 36,636 950 16.37 97% 98% 97% 97%
n-body
source secs mem gz busy cpu load
Swift 21.94 12,152 1355 27.62 100% 8% 12% 6%
Java 21.91 35,356 1429 22.28 100% 0% 0% 1%
reverse-complement
source secs mem gz busy cpu load
Swift 3.71 789,516 1286 4.49 38% 2% 0% 81%
Java 3.26 738,800 2183 7.49 44% 63% 73% 48%
fasta
source secs mem gz busy cpu load
Swift 3.11 8,680 1945 4.71 45% 48% 16% 43%
Java 2.24 44,876 2473 6.13 18% 79% 79% 97%
k-nucleotide
source secs mem gz busy cpu load
Swift 22.07 257,292 1367 83.89 99% 94% 94% 94%
Java 9.06 435,240 1812 28.35 98% 73% 72% 69%
regex-redux
source secs mem gz busy cpu load
Swift 45.10 882,596 715 79.79 14% 79% 84% 0%
Java 10.25 634,884 740 30.23 68% 95% 67% 65%
binary-trees
source secs mem gz busy cpu load
Swift 45.43 733,576 714 134.65 68% 63% 91% 74%
Java 8.29 966,764 835 27.76 86% 80% 86% 83%
Swift

Swift version 5.1 (swift-5.1-RELEASE)
Target: x86_64-unknown-linux-gnu

Java

openjdk 13 2019-09-17
OpenJDK Runtime Environment (build 13+33)
OpenJDK 64-Bit Server VM (build 13+33, mixed mode, sharing)