Skip to content

Instantly share code, notes, and snippets.

@AvverbioPronome
Last active January 17, 2026 12:12
Show Gist options
  • Select an option

  • Save AvverbioPronome/431d1b10eccecf6238369c0ed8669d24 to your computer and use it in GitHub Desktop.

Select an option

Save AvverbioPronome/431d1b10eccecf6238369c0ed8669d24 to your computer and use it in GitHub Desktop.
Dragracing dotproducts
*
!*/
!*.*
![Mm]akefile
*.o
*.hi
-- We use Int (machine integer) to match Numba's speed.
-- If you want exact big math, keep 'Integer', but it will be slower.
main :: IO ()
main = do
let n = 2^30 :: Integer
-- print forces the calculation
print $ sum $ zipWith (*) [1..n] [1..n]
// compile with: gcc -O3 -fopenmp endgame.c -o endgame
#include <stdio.h>
#include <omp.h>
int main() {
long long n = 1L << 30; // 2^30
double total = 0.0;
// The Magic Pragma
// "parallel": Spawn threads
// "for": Split the loop iterations among them
// "reduction(+:total)": Give each thread a local sum, then merge them
#pragma omp parallel for reduction(+:total)
for (long long i = 0; i < n; i++) {
double val = (double)i;
total += val * val;
}
printf("%e\n", total);
return 0;
}
CC = gcc
HC = ghc
CFLAGS = -O3 -fopenmp
HFLAGS = -O2
.PHONY = test all
all: drag endgame test
%: %.c
$(CC) $(CFLAGS) $< -o $@
%: %.hs
$(HC) $(HFLAGS) $< -o $@
test: drag endgame slow.py
zsh -c "time ./slow.py"
zsh -c "time ./drag"
zsh -c "time ./endgame"
#!/usr/bin/env python3
xs=range(0,2**30)
ys=range(0,2**30)
sum((x*y for x,y in zip(xs, ys)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment