Skip to content

Instantly share code, notes, and snippets.

@vojtamolda
Forked from joaqo/sumlist.c
Last active November 28, 2021 06:50
Show Gist options
  • Save vojtamolda/7225a6494a88921e7dc01100f5a64177 to your computer and use it in GitHub Desktop.
Save vojtamolda/7225a6494a88921e7dc01100f5a64177 to your computer and use it in GitHub Desktop.
Simple Benchmark of Summation in C and Swift
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#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;
}
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