Last active
May 9, 2016 15:40
-
-
Save airspeedswift/dffe6dd19a4bf6ef91b5 to your computer and use it in GitHub Desktop.
Swift vs C Prime Number Benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 Swift"); | |
for (int index = primeCount-11; index < primeCount; index++) | |
{ | |
int aPrime = primes[index]; | |
printf("Prime[%d] = %d\n",index,aPrime); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
println("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(primes.count, startTime) | |
} | |
} | |
//Move on to the next (odd) number. | |
candidate += 2 | |
} | |
updateTotal(primes.count, startTime) | |
//println("Checking for swift completion block") | |
//print the last 10 primes calculated | |
println("\nPrimes calculated in Swift") | |
for idx in (primes.endIndex-11)..<primes.endIndex | |
{ | |
println("Prime[\(idx)] = \(primes[idx])") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/src [1] % swiftc --version | |
Apple Swift version 1.2 (swiftlang-602.0.47.4 clang-602.0.48) | |
Target: x86_64-apple-darwin14.3.0 | |
~/src [2] % swiftc -O -o prime_swift prime.swift | |
~/src [3] % ./prime_swift | |
Total time: 0.499701023101807, calculated: 262144, per second: 524601.687570674 | |
Total time: 1.30302101373672, calculated: 524288, per second: 402363.426585484 | |
Total time: 2.25548201799393, calculated: 786432, per second: 348675.801325816 | |
Total time: 3.38296902179718, calculated: 1048576, per second: 309957.31655945 | |
Total time: 4.62443399429321, calculated: 1310720, per second: 283433.605413656 | |
Total time: 5.96368700265884, calculated: 1572864, per second: 263740.199527366 | |
Total time: 7.38956499099731, calculated: 1835008, per second: 248324.225070838 | |
Total time: 8.30392301082611, calculated: 2000000, per second: 240850.017201813 | |
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 | |
~/src [4] % cc --version | |
Apple LLVM version 6.1.0 (clang-602.0.48) (based on LLVM 3.6.0svn) | |
Target: x86_64-apple-darwin14.3.0 | |
Thread model: posix | |
~/src [5] % cc -Ofast -framework CoreFoundation -o prime_c prime.c | |
~/src [6] % ./prime_c | |
Total time: 0.443690, calculated: 262144, per second: 590826.926095 | |
Total time: 1.189520, calculated: 524288, per second: 440755.934644 | |
Total time: 2.095344, calculated: 786432, per second: 375323.573297 | |
Total time: 3.170024, calculated: 1048576, per second: 330778.564339 | |
Total time: 4.350820, calculated: 1310720, per second: 301258.153293 | |
Total time: 5.646417, calculated: 1572864, per second: 278559.658973 | |
Total time: 7.035104, calculated: 1835008, per second: 260835.943651 | |
Total time: 7.955803, calculated: 2000000, per second: 251388.827850 | |
Primes calculated in SwiftPrime[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 | |
~/src [7] % |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment