Understand the performance characteristics of weightedrand
mainThe weightedrand library is optimized for repeated selections from the same set of choices. It achieves this by creating a presorted cache that allows for fast binary search selection.
When to use weightedrand vs randutil
- Use
weightedrandwhen you need to perform many repeated samplings from a large collection. It is significantly faster for repeated calls but incurs higher initialization time and memory usage. - Use
github.com/jmcvetta/randutilif you are only performing a single selection from a distribution. It is optimized for the single-operation case.
Parallelism
Starting from v0.3.0, weightedrand can efficiently utilize a single Chooser across multiple CPU cores in parallel, increasing overall throughput for high-concurrency workloads.