-
-
Save vojtamolda/7225a6494a88921e7dc01100f5a64177 to your computer and use it in GitHub Desktop.
Simple Benchmark of Summation in C and Swift
This file contains 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
#include <time.h> | |
#include <stdio.h> | |
int main () { | |
const int N = 300000; | |
int64_t buffer[N]; | |
for (int test = 0; test < 15; test++) { | |
clock_t start = clock(); | |
for (int i = 0; i < N; i++) { | |
buffer[i] = test; | |
} | |
int64_t sum = 0; | |
for (int i = 0; i < N; i++) { | |
sum += buffer[i]; | |
} | |
printf("%.3f ms %llu\n", ((double)(clock() - start)) / CLOCKS_PER_SEC * 1000, sum); | |
} | |
return 0; | |
} |
This file contains 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
import Foundation | |
let N = 300_000 | |
var buffer = [Int64](repeating: 0, count: N) | |
for test: Int64 in 0 ..< 15 { | |
let start = Date() | |
buffer.withUnsafeMutableBufferPointer { buffer in | |
for i in 0 ..< N { | |
buffer[i] = test | |
} | |
} | |
let sum = buffer.reduce(0, &+) | |
print("\(String(format: "%.3f", Date().timeIntervalSince(start) * 1_000)) ms \(sum)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment