Given a list of posts, compute the top 5 related posts for each post based on the number of shared tags.
- Read the posts JSON file.
- Iterate over the posts and populate a map containing:
tag -> List<int>
, with the int representing the post index of each post with that tag.
- Iterate over the posts and for each post:
- Create a map:
PostIndex -> int
to track the number of shared tags
- For each tag, Iterate over the posts that have that tag
- For each post, increment the shared tag count in the map.
- Sort the related posts by the number of shared tags.
- Write the top 5 related posts for each post to a new JSON file.
./run.sh go | rust | python | all
# windows (powershell)
./run.ps1 go | rust | python | all
or
pwsh ./run.ps1 go | rust | python | all
Rules
- FFI (including assembly inlining)
- Unsafe code blocks
- Custom benchmarking
- Disabling runtime checks (bounds etc)
- Parse json at runtime
- Not hardcode number of posts
- Support up to 100 tags
- Use a stable release of the compiler/runtime
Updated Results from github workflow (raw data)
VM Specs
NB: The benchmark runs on the free tier of github workflow.
- CPU: 2 vCPUs
- RAM: 7GB
- OS: Ubuntu 22.04
Source
Language |
Processing Time |
Total (PT + I/O) |
Zig |
29.23 ms |
71.3 ms |
Go |
30.39 ms |
61.3 ms |
Java (GraalVM) |
36.00 ms |
61.4 ms |
Rust |
39.11 ms |
53.7 ms |
Odin |
45.04 ms |
326.6 ms |
Vlang |
46.12 ms |
335.8 ms |
Nim |
46.50 ms |
71.3 ms |
F# |
48.00 ms |
287.1 ms |
Swift |
48.79 ms |
402.5 ms |
Crystal |
58.49 ms |
105.3 ms |
Dart VM |
77.00 ms |
501.0 ms |
C# |
97.63 ms |
298.8 ms |
LuaJIT |
101.77 ms |
351.2 ms |
Dart AOT |
115.75 ms |
239.5 ms |
JS (Deno) |
149.00 ms |
233.7 ms |
JS (Node) |
162.40 ms |
230.0 ms |
Java (JIT) |
235.69 ms |
496.3 ms |
Numpy |
0.39 s |
596.4 ms |
Julia v2 |
584.83 ms |
4.770 s |
JS (Bun) |
638.60 ms |
700.9 ms |
Lua |
1956.41 ms |
2.534 s |
Python |
2.56 s |
2.622 s |
Language |
Processing Time |
Total (PT + I/O) |
Rust Concurrent |
19.59 ms |
35.5 ms |
Go Concurrent |
22.04 ms |
50.9 ms |
Swift Concurrent |
30.25 ms |
392.7 ms |
F# Concurrent |
41.57 ms |
787.6 ms |
Old Results with details (on my machine)
Language |
Processing Time |
Total (+ I/O) |
Details |
Rust |
- |
4.5s |
Initial |
Rust v2 |
- |
2.60s |
Replace std HashMap with fxHashMap by phazer99 |
Rust v3 |
- |
1.28s |
Preallocate and reuse map and unstable sort by vdrmn and Darksonn |
Rust v4 |
- |
0.13s |
Use Post index as key instead of Pointer and Binary Heap by RB5009 |
Rust v5 |
38ms |
52ms |
Rm hashing from loop and use vec[count] instead of map[index]count by RB5009 |
Rust v6 |
23ms |
36ms |
Optimized Binary Heap Ops by scottlamb |
Rust Rayon |
9ms |
22ms |
Parallelize by masmullin2000 |
Rust Rayon |
8ms |
22ms |
Remove comparison out of hot loop |
⠀ |
⠀ |
⠀ |
⠀ |
Go |
- |
1.5s |
Initial |
Go v2 |
- |
80ms |
Add rust optimizations |
Go v3 |
56ms |
70ms |
Use goccy/go-json |
Go v3 |
34ms |
55ms |
Use generic binaryheap by DrBlury |
Go v4 |
26ms |
50ms |
Replace binary heap with custom priority queue |
Go v5 |
20ms |
43ms |
Remove comparison out of hot loop |
Go Con |
10ms |
33ms |
Go concurrency by tirprox and DrBlury |
Go Con v2 |
5ms |
29ms |
Use arena, use waitgroup, rm binheap by DrBlury |
⠀ |
⠀ |
⠀ |
⠀ |
Python |
- |
7.81s |
Initial |
Python v2 |
1.35s |
1.53s |
Add rust optimizations by dave-andersen |
Numpy |
0.57s |
0.85s |
Numpy implementation by Copper280z |
⠀ |
⠀ |
⠀ |
⠀ |
Crystal |
50ms |
96ms |
Inital w/ previous optimizations |
Crystal v2 |
33ms |
72ms |
Replace binary heap with custom priority queue |
⠀ |
⠀ |
⠀ |
⠀ |
Odin |
110ms |
397ms |
Ported from golang code |
Odin v2 |
104ms |
404ms |
Remove comparison out of hot loop |
⠀ |
⠀ |
⠀ |
⠀ |
Dart VM |
125ms |
530ms |
Ported frog golang code |
Dart bin |
274ms |
360ms |
Compiled executable |
⠀ |
⠀ |
⠀ |
⠀ |
Vlang |
339ms |
560ms |
Ported from golang code |
⠀ |
⠀ |
⠀ |
⠀ |
Zig |
80ms |
110ms |
Provided by akhildevelops |