I conducted tests with a stopwatch. 100,000 iterations:
System.Random rnd = new System.Random(); if (rnd.Next(2) == 0) trues++;
Processors love integers, so the Next (2) method was faster. 3700 vs 7500 ms, which is very significant. Also: I think random numbers can be a bottleneck, I created about 50 of every frame in Unity, even with a tiny scene that noticeably slows down my system, so I also hoped to find a method to create a random boolean. So I also tried
if (System.DateTime.Now.Millisecond % 2 == 0) trues++;
but the call to the static function was even slower with 9600 ms. Worth a shot. Finally, I skipped the comparison and created only 100,000 random values ββto make sure that the comparison of int and double did not affect the elapsed time, but the result was almost the same.
Frederik Steinmetz Dec 13 '18 at 12:44 2018-12-13 12:44
source share