Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks comparing random_mod and random_bits #703

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dvdplm
Copy link
Contributor

@dvdplm dvdplm commented Nov 19, 2024

@tarcieri I noticed that generating random numbers using random_mod is slower than using random_bits repeatedly until the random number is below the given bound, like so:

            let bound = rng.next_u64();
            let bound_bits = bound.bits_vartime();

            let mut r = U1024::random_bits(&mut rng, bound_bits);
            while r >= bound {
                r = U1024::random_bits(&mut rng, bound_bits);
            }

The benchmarks show that random_mod is ~4x slower than random_bits.

It's not obvious to me why this is the case and perhaps it is to be expected. Is this something that is worth investigating further?

     Running benches/uint.rs (target/release/deps/uint-e0f5a60c64b6a49c)
bounded random/random_mod, U1024
                        time:   [533.35 ns 919.84 ns 1.4034 µs]
                        change: [-52.125% -13.878% +49.260%] (p = 0.64 > 0.05)
                        No change in performance detected.
Found 12 outliers among 100 measurements (12.00%)
  6 (6.00%) high mild
  6 (6.00%) high severe
bounded random/brute force, U1024
                        time:   [216.74 ns 229.78 ns 243.08 ns]
                        change: [-6.0962% -0.2957% +5.4555%] (p = 0.93 > 0.05)
                        No change in performance detected.
bounded random/brute force, U1024, small bound
                        time:   [28.588 ns 31.115 ns 33.674 ns]
                        change: [-8.3725% -0.7117% +8.0549%] (p = 0.87 > 0.05)
                        No change in performance detected.
bounded random/random_mod, U1024, small bound
                        time:   [63.862 ns 118.93 ns 204.96 ns]
Found 11 outliers among 100 measurements (11.00%)
  3 (3.00%) high mild
  8 (8.00%) high severe
bounded random/brute force, U1024, 512 bit bound low
                        time:   [102.46 ns 108.57 ns 114.95 ns]
                        change: [-6.2214% -0.2501% +5.6826%] (p = 0.93 > 0.05)
                        No change in performance detected.
bounded random/random_mod, U1024, 512 bit bound low
                        time:   [373.52 ns 825.42 ns 1.6169 µs]
Found 15 outliers among 100 measurements (15.00%)
  2 (2.00%) high mild
  13 (13.00%) high severe
bounded random/brute force, U1024, 512 bit bound hi
                        time:   [203.00 ns 216.22 ns 230.30 ns]
                        change: [-3.7858% +2.6058% +9.1138%] (p = 0.43 > 0.05)
                        No change in performance detected.
bounded random/random_mod, U1024, 512 bit bound hi
                        time:   [557.52 ns 729.76 ns 933.31 ns]
Found 8 outliers among 100 measurements (8.00%)
  1 (1.00%) high mild
  7 (7.00%) high severe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant