G
G
GrigorySvetov2018-07-19 22:11:12
.NET
GrigorySvetov, 2018-07-19 22:11:12

Are cryptographically safe numbers in .NET faster than System.Random()?

Please explain why (at least in tests on PascalABC.NET) code #1 is ~3 times faster than code #2?

//№1
var
  arr: array of byte;
  rndg := new System.Security.Cryptography.RNGCryptoServiceProvider();

begin
  setlength(arr, 10000000);
  rndg.GetBytes(arr);
  write(milliseconds());//от 50 до 70
end.

//№2
var
  arr: array of byte;
  rnd := new System.Random();

begin
  setlength(arr, 10000000);
  rnd.NextBytes(arr);
  write(milliseconds);//от 160 до 200
end.

Why does the generation of crypto-secure (according to the .NET documentation) random ones require significantly less time resources than the standard ones?
(when the array length is increased to 100 million elements, it takes ~ 0.3 s for crypto-random elements, and about 1.5 s for standard ones)
(by the way: when testing 1 billion crypto-random elements, significant memory losses are noticeable, although everything turned out to be calculated in 41 s; in a similar test, 1 billion standard-random was spent 18 seconds (SUDDENLY), and there were no hang-ups in terms of memory waste (I did not measure the memory, just a general feeling from the very "behavior" of the computer)).
In total, I change the question: I just ask you to comment on this behavior of .NET (is it .NET ???), explaining it if possible; including interesting "kink" for 1 billion bytes.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question