Skip to content

Instantly share code, notes, and snippets.

@KarlAmort
Forked from airspeedswift/prime.c
Last active May 9, 2016 16:18
Show Gist options
  • Save KarlAmort/d1f19a11d539e609fb16fc0d4d048d07 to your computer and use it in GitHub Desktop.
Save KarlAmort/d1f19a11d539e609fb16fc0d4d048d07 to your computer and use it in GitHub Desktop.
Swift vs C Prime Number Benchmark
// code originally adapted from https://github.com/DuncanMC/SwiftPerformanceBenchmark
#include <stdlib.h>
#include <stdio.h>
#include <CoreFoundation/CFDate.h>
void updateTotal(int newTotal, CFAbsoluteTime startTime) {
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime totalTime = now - startTime;
printf("Total time: %f, calculated: %d, per second: %f\n", totalTime, newTotal, newTotal / totalTime);
}
int main(int argc, const char * argv[]) {
int totalCount = 2000000;
int primeCount = 1;
int *primes = (int *)malloc(sizeof(int) * totalCount);
primes[0] = 2;
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
int candidate = 3;
int isPrime = 0;
while (primeCount < totalCount)
{
isPrime = 1;
//Loop through all the primes we've already found.
for (int i = 0; i < primeCount; ++i)
{
int oldPrime = primes[i];
if (candidate % oldPrime == 0)
{
isPrime = 0;
break;
}
if (candidate < oldPrime * oldPrime)
{
isPrime = 1;
break;
}
}
if (isPrime)
{
primes[primeCount++] = candidate;
if ((primeCount & 0x3ffff) == 0)
{
updateTotal(primeCount, startTime);
}
}
//Move on to the next (odd) number.
candidate += 2;
}
updateTotal(primeCount, startTime);
printf("\nPrimes calculated in C");
for (int index = primeCount-12; index < primeCount; index++)
{
int aPrime = primes[index];
printf("Prime[%d] = %d\n",index,aPrime);
}
return 0;
}
// code originally adapted from https://github.com/DuncanMC/SwiftPerformanceBenchmark
import CoreFoundation.CFDate
func updateTotal(newTotal: Int, startTime: CFAbsoluteTime) {
let now = CFAbsoluteTimeGetCurrent()
let totalTime = now - startTime
print("Total time: \(totalTime), calculated: \(newTotal), per second: \(Double(newTotal)/totalTime)")
}
let totalCount = 2_000_000
var primes: [CInt] = []
primes.reserveCapacity(totalCount)
primes.append(2)
let startTime = CFAbsoluteTimeGetCurrent()
var candidate: CInt = 3
var isPrime: Bool = false
while primes.count < totalCount
{
isPrime = true
//Loop through all the primes we've already found.
for oldPrime in primes
{
if candidate % oldPrime == 0
{
isPrime = false
break
}
if candidate < oldPrime &* oldPrime
{
isPrime = true
break
}
}
if isPrime
{
primes.append(candidate)
if (primes.count & 0x3ffff) == 0
{
updateTotal(newTotal: primes.count, startTime: startTime)
}
}
//Move on to the next (odd) number.
candidate += 2
}
updateTotal(newTotal: primes.count, startTime: startTime)
//print the last 10 primes calculated
print("\nPrimes calculated in Swift")
for idx in (primes.endIndex-11)..<primes.endIndex
{
print("Prime[\(idx)] = \(primes[idx])")
}
$>$> git clone https://gist.github.com/d1f19a11d539e609fb16fc0d4d048d07.git
$> xcrun -sdk macosx swiftc -v
Apple Swift version 3.0-dev (LLVM dffa09ffd8, Clang 1e6cba3ce3, Swift d2aee43220)
Target: x86_64-apple-macosx10.9
$> xcrun -sdk macosx swiftc -Ounchecked prime.swift
$> ./prime
Total time: 0.168334007263184, calculated: 262144, per second: 1557284.854451
Total time: 0.439334034919739, calculated: 524288, per second: 1193369.86968419
Total time: 0.775188982486725, calculated: 786432, per second: 1014503.58269697
Total time: 1.1577240228653, calculated: 1048576, per second: 905721.898561662
Total time: 1.58434802293777, calculated: 1310720, per second: 827292.981733647
Total time: 2.04609698057175, calculated: 1572864, per second: 768714.296015671
Total time: 2.54207599163055, calculated: 1835008, per second: 721854.109020155
Total time: 2.87144201993942, calculated: 2000000, per second: 696514.150768816
Primes calculated in Swift
Prime[1999989] = 32452649
Prime[1999990] = 32452657
Prime[1999991] = 32452681
Prime[1999992] = 32452687
Prime[1999993] = 32452727
Prime[1999994] = 32452759
Prime[1999995] = 32452781
Prime[1999996] = 32452789
Prime[1999997] = 32452837
Prime[1999998] = 32452841
Prime[1999999] = 32452843
$> /usr/local/Cellar/gcc/5.3.0/bin/gcc-5 -v
Using built-in specs.
COLLECT_GCC=/usr/local/Cellar/gcc/5.3.0/bin/gcc-5
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/5.3.0/libexec/gcc/x86_64-apple-darwin15.0.0/5.3.0/lto-wrapper
Target: x86_64-apple-darwin15.0.0
Configured with: ../configure --build=x86_64-apple-darwin15.0.0 --prefix=/usr/local/Cellar/gcc/5.3.0 --libdir=/usr/local/Cellar/gcc/5.3.0/lib/gcc/5 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-5 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --with-build-config=bootstrap-debug --disable-werror --with-pkgversion='Homebrew gcc 5.3.0' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --enable-multilib
Thread model: posix
gcc version 5.3.0 (Homebrew gcc 5.3.0)
$> /usr/local/Cellar/gcc/5.3.0/bin/gcc-5 -Ofast -framework CoreFoundation -o prime_c prime.c
$> ./prime_c
Total time: 0.185617, calculated: 262144, per second: 1412284.209431
Total time: 0.485510, calculated: 524288, per second: 1079870.537489
Total time: 0.857549, calculated: 786432, per second: 917069.449400
Total time: 1.287324, calculated: 1048576, per second: 814539.300731
Total time: 1.767529, calculated: 1310720, per second: 741555.013814
Total time: 2.292153, calculated: 1572864, per second: 686195.031235
Total time: 2.848667, calculated: 1835008, per second: 644163.738173
Total time: 3.219280, calculated: 2000000, per second: 621256.926146
Primes calculated in C
Prime[1999988] = 32452631
Prime[1999989] = 32452649
Prime[1999990] = 32452657
Prime[1999991] = 32452681
Prime[1999992] = 32452687
Prime[1999993] = 32452727
Prime[1999994] = 32452759
Prime[1999995] = 32452781
Prime[1999996] = 32452789
Prime[1999997] = 32452837
Prime[1999998] = 32452841
Prime[1999999] = 32452843
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment