Last active
January 17, 2026 12:12
-
-
Save AvverbioPronome/431d1b10eccecf6238369c0ed8669d24 to your computer and use it in GitHub Desktop.
Dragracing dotproducts
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
| * | |
| !*/ | |
| !*.* | |
| ![Mm]akefile | |
| *.o | |
| *.hi |
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
| -- 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] |
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
| // 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; | |
| } |
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
| 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" |
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
| #!/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