Random.NextBytes Performance Issues
I have became aware recently to a performance problem with the Random class, specifically the Random.NextBytes method.
As it turns out this is an expansive operation:
The graph displays the number of milliseconds used to perform 10,000,000 operations.
The NextBytes method handle a byte array containing only 4 bytes.
This is linear - using an array of 4000 bytes will take 1000 times longer to complete the operation!
However, there may be a work-around using the BitConverter class.
Use BitConverter.GetBytes(Random.NextDouble()) to get a randomized array of 8 bytes.
Here are the results:
As you can see, generating an array of 8 bytes this way takes half the time of generating it using the NextBytes method.
No comments:
Post a Comment