Last active
June 12, 2018 00:25
-
-
Save yang/3099144dccc734692a40ed44d8f86a23 to your computer and use it in GitHub Desktop.
Silly array benchmarks in diff languages
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
Ran on a 2015 MBP | |
HOTSPOT | |
public class Main { | |
public static void main(String[] args) { | |
int[] a = new int[500000000]; | |
int[] b = new int[60000000]; | |
for (int i = 0; i < a.length; i++) { | |
a[i] = i; | |
} | |
for (int trial = 0; trial < 2; trial++) { | |
long start = System.currentTimeMillis(); | |
int j = 0; | |
long sum = 0; | |
for (int i = 0; i < a.length; i++) { | |
sum += a[i]; | |
if ((i+1) % 10 == 0) { | |
b[j++] = (int) sum; | |
sum = 0; | |
} | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println("last sum is " + b[j-1] + "; took " + (end - start) + " ms"); | |
} | |
} | |
} | |
last sum is 705032649; took 1079 ms | |
last sum is 705032649; took 1339 ms | |
NODE | |
function main() { | |
//const a = []; | |
//a.length = 5000000000; | |
//a.fill(0); | |
//const b = []; | |
//b.length = 600000000; | |
//b.fill(0); | |
const a = new Uint32Array(500000000); | |
const b = new Uint32Array(60000000); | |
for (let i = 0; i < a.length; i++) { | |
a[i] = i; | |
} | |
for (let i = 0; i < b.length; i++) { | |
b[i] = i; | |
} | |
for (let trial = 0; trial < 2; trial++) { | |
let sum = 0, j = 0; | |
const start = +Date.now(); | |
for (let i = 0; i < a.length; i++) { | |
sum += a[i]; | |
if ((i+1) % 10 == 0) { | |
b[j++] = sum; | |
sum = 0; | |
} | |
} | |
const end = +Date.now(); | |
console.log("last sum is " + b[j-1] + "; took " + (end - start) + " ms"); | |
} | |
} | |
main(); | |
last sum is 705032649; took 1654 ms | |
last sum is 705032649; took 1557 ms | |
CLANG++ | |
#include <chrono> | |
#include <iostream> | |
int main() { | |
const int ALEN = 500000000; | |
const int BLEN = 60000000; | |
int* a = new int[ALEN]; | |
int *b = new int[60000000]; | |
for (int i = 0; i < ALEN; i++) { | |
a[i] = i; | |
} | |
for (int i = 0; i < BLEN; i++) { | |
b[i] = i; | |
} | |
for (int trial = 0; trial < 2; trial++) { | |
int sum = 0, j = 0; | |
using namespace std::chrono; | |
using namespace std; | |
milliseconds start = duration_cast< milliseconds >( | |
system_clock::now().time_since_epoch() | |
); | |
for (int i = 0; i < ALEN; i++) { | |
sum += a[i]; | |
if ((i+1) % 10 == 0) { | |
b[j++] = sum; | |
sum = 0; | |
} | |
} | |
milliseconds end = duration_cast< milliseconds >( | |
system_clock::now().time_since_epoch() | |
); | |
cout << "last sum is " << b[j-1] << "; took " << (end.count() - start.count()) << " ms" << endl; | |
} | |
} | |
clang++ main.cc | |
./a.out | |
last sum is 705032649; took 2380 ms | |
last sum is 705032649; took 2313 ms | |
clang++ -O3 main.cc | |
./a.out | |
last sum is 705032649; took 717 ms | |
last sum is 705032649; took 1016 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment