I need something faster than BigInteger.probablePrime
i have an method:
the result of which:
this.bitLength()
is always 2048
this.getRandom()
is always new SecureRandom()20 Replies
⌛
This post has been reserved for your question.
Hey @swimer! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
This is not a valid benchmark!
If you want to create proper benchmarks, you should use JMH
Java specifically optimizes code that is executed frequently
Well, the problem is that it takes a long time to complete
otherwise you'll get wrong measurements
it's slow initially but it gets faster when calling it often
are there no more stable methods in java?
im using jdk 21
What do you mean with stable?
I want to generate a number as quickly as possible and ideally without the "slowly at first" conditions
CDS could help with that
but it's a bit difficult to set up
For testing: Can you try with
Random
instead of SecureRandom
? I want to know whether the SecureRandom
or the actual BigInteger.probablyPrime
os tje ossie
CDS can improve startup time by making stuff faster to load
though I don't think it would do much for your BigInteger
test
(without the AOT cache)new Random()
takes 1083ms
ThreadLocalRandom.current()
takes 608msand how long does the
BigInteger.probablePrime
take with those?it's probablePrime with them
then the issue is the random number generation
Are you on Windows?
the creation of an instance is performed once at startup, so it does not matter how long it takes to create a random
yes
exactly
you might be able to pick a default
SecureRandom
RNGI would like to generate a 2048 bit number, but it takes too long. I could roughly calculate how many bits 1 digit takes and generate it through for, but is it correct?
I'm not sure that generating each number separately will be faster
there are different SecureRandom providers
https://docs.oracle.com/en/java/javase/21/docs/specs/security/standard-names.html#securerandom-number-generation-algorithms
Try the following:
or use Linux :p
I'm just making a server that will be on Linux in the future
Windows-PRNG
- 3200ms
SHA1PRNG
- 1400ms
PKCS11
- not availableand the others?
1400ms is pretty good
RANDOM.ORG - True Random Number Service
RANDOM.ORG offers true random numbers to anyone on the Internet. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.