Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #11831

Re: Fast randomizer

Newsgroups comp.programming
Date 2021-01-18 03:08 -0800
References (2 earlier) <0ace59de-aecc-4ac8-ae40-b9394a913fc4n@googlegroups.com> <72f1bfaa-a97c-4bec-a999-c7ebdb6b4d3fn@googlegroups.com> <ru2g36$3ge$1@dont-email.me> <39288c53-9bbf-4c3b-96e6-f2b93991497dn@googlegroups.com> <ru3eeg$9ke$1@dont-email.me>
Message-ID <137c1d41-e746-4835-9dda-8ca5dc4002adn@googlegroups.com> (permalink)
Subject Re: Fast randomizer
From H e <he12457@hotmail.com>

Show all headers | View raw


David Brown kirjutas esmaspäev, 18. jaanuar 2021 kl 09:49:28 UTC+2:
> On 18/01/2021 01:53, H e wrote: 
> > David Brown kirjutas esmaspäev, 18. jaanuar 2021 kl 01:09:30 UTC+2: 
> >> On 17/01/2021 23:13, H e wrote: 
> >>> Seems a bit faster in Min. 
> >> (This is your chance to be viewed as serious about learning to program. 
> >> Think, and reply carefully, and you have a chance to learn something 
> >> useful. Continue to post the verbal diarrhoea that you've done so far 
> >> in this group, and you'll lose that chance.) 
> >> 
> >> 
> >> What do you think you are testing, using the code you posted? How do 
> >> you think it is affected by the speed of the Math.random() function? 
> > 
> > Hello dear David. 
> > I am testing random() speed.
> No - that is what you /want/ to test, but it is certainly not what you 
> are doing with that code. 
> 
> Try with code like this: 
> 
> 
> <!DOCTYPE html> 
> <html> 
> <body> 
> 
> <h2>Timing of Math.random()</h2> 
> 
> <p>EmptyTime: <span id = "EmptyTime">X</span></p> 
> <p>TotalTime: <span id = "TotalTime">X</span></p> 
> <p>RandTime: <span id = "RandTime">X</span></p> 
> 
> <script> 
> 
> const rounds = 1000000000; 
> 
> var starttime = Date.now() 
> for (var i = 0; i < rounds; i++) { 
> // Empty loop 
> } 
> var emptytime = Date.now() - starttime; 
> 
> document.getElementById("EmptyTime").innerHTML = 
> (1000000 * emptytime / rounds) + " µs"; 
> 
> starttime = Date.now() 
> for (var i = 0; i < rounds; i++) { 
> var x = Math.random(); 
> } 
> var totaltime = Date.now() - starttime; 
> 
> document.getElementById("TotalTime").innerHTML = 
> 1000000 * (totaltime / rounds) + " µs"; 
> 
> var randtime = totaltime - emptytime; 
> document.getElementById("RandTime").innerHTML = 
> 1000000 * (randtime / rounds) + " µs"; 
> 
> 
> </script> 
> 
> </body> 
> </html> 
> 
> 
> Depending on the speed of your computer/browser, you may need to change 
> the value of "rounds". The script should take a few seconds to run - 
> anything less and you have little accuracy in the results. Ideally it 
> should run for minutes, but I didn't want to spend that long. 
> 
> Now, let's compare my code with yours, and see what differences there are. 
> 
> 1. My code looks like program code, not line noise. Learn to lay out 
> your code properly with indentation, and spacing in the code. 
> Readability is key to programming. (If Richard is paying attention, 
> I'll let him quote you Heathfield's Law here.) Use suitable identifier 
> names, even in the smallest of test code. But don't fill the code with 
> pointless style stuff - if all you need out is some numbers, that's all 
> you make. 
> 
> 2. If you want to test the speed of something - like the Math.random() 
> function - then aim to test /only/ that. Your code does rounding, 
> arithmetic, conversion to string, string concatenation - which will take 
> orders of magnitude longer than the call to Math.random(), completely 
> swamping its effect. 
> 
> 3. When testing the speed of a fast operation, you need to do it /many/ 
> times. Here I am looping 1,000,000,000 times. You looped 10 times. 
> PC's, especially JavaScript environments, have a huge variability in 
> their timings depending on loads, dynamic clock speeds, JIT 
> optimisation, and all sorts of other factors. To have a hope of even a 
> vaguely useful result, you need to average out over a huge number of 
> iterations. 
> 
> 4. The speed of the "for" loop itself could dominate. (In my testing, 
> it was in fact roughly the same speed as the call to Math.random() 
> itself.) So my code compensates for that. 
> 
> 
> So now you have a rough basis for doing some benchmarking that can give 
> at least vaguely realistic results, you do some more testing. Things 
> you will want to check include : 
> 
> 1. Is Date.now() the best way of getting a good time reference? (I 
> don't know, I am not a JavaScript expert.) 
> 
> 2. Is the timing stable enough, or can you do better? Take multiple 
> samples and present a table or graph? 
> 
> 3. How does simply calling an empty function compare to calling 
> Math.round() ? If it is similar, then Math.random() is already as fast 
> as it could be. 
> 
> 4. How does your own "random" function compare in timing?

This code does not work in my browser.





He

372(country code) 6861327

Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Fast randomizer H e <he12457@hotmail.com> - 2021-01-17 12:59 -0800
  Re: Fast randomizer Tavis Ormandy <taviso@gmail.com> - 2021-01-17 21:11 +0000
    Re: Fast randomizer H e <he12457@hotmail.com> - 2021-01-17 13:26 -0800
      Re: Fast randomizer H e <he12457@hotmail.com> - 2021-01-17 14:01 -0800
      Re: Fast randomizer H e <he12457@hotmail.com> - 2021-01-17 14:13 -0800
        Re: Fast randomizer David Brown <david.brown@hesbynett.no> - 2021-01-18 00:09 +0100
          Re: Fast randomizer H e <he12457@hotmail.com> - 2021-01-17 16:53 -0800
            Re: Fast randomizer Richard Heathfield <rjh@cpax.org.uk> - 2021-01-18 07:30 +0000
              Re: Fast randomizer David Brown <david.brown@hesbynett.no> - 2021-01-18 08:55 +0100
                Re: Fast randomizer Richard Heathfield <rjh@cpax.org.uk> - 2021-01-18 08:05 +0000
            Re: Fast randomizer David Brown <david.brown@hesbynett.no> - 2021-01-18 08:47 +0100
              Re: Fast randomizer H e <he12457@hotmail.com> - 2021-01-18 03:08 -0800
                Re: Fast randomizer H e <he12457@hotmail.com> - 2021-01-18 03:18 -0800
                Re: Fast randomizer David Brown <david.brown@hesbynett.no> - 2021-01-18 12:19 +0100
                Re: Fast randomizer Julio Di Egidio <julio@diegidio.name> - 2022-01-05 02:56 -0800
                Re: Fast randomizer Richard Heathfield <rjh@cpax.org.uk> - 2022-01-05 11:27 +0000
                Re: Fast randomizer David Brown <david.brown@hesbynett.no> - 2022-01-05 16:00 +0100
                Re: Fast randomizer Richard Heathfield <rjh@cpax.org.uk> - 2022-01-05 16:35 +0000
                Re: Fast randomizer Julio Di Egidio <julio@diegidio.name> - 2022-01-05 14:23 -0800
                Re: Fast randomizer David Brown <david.brown@hesbynett.no> - 2022-01-05 23:36 +0100
                Re: Fast randomizer Julio Di Egidio <julio@diegidio.name> - 2022-01-05 14:41 -0800
                Re: Fast randomizer Richard Heathfield <rjh@cpax.org.uk> - 2022-01-06 00:32 +0000
                Re: Fast randomizer Julio Di Egidio <julio@diegidio.name> - 2022-01-06 01:21 -0800
                Re: Fast randomizer Richard Heathfield <rjh@cpax.org.uk> - 2022-01-06 09:38 +0000
                Re: Fast randomizer Julio Di Egidio <julio@diegidio.name> - 2022-01-06 01:48 -0800
                Re: Fast randomizer Richard Heathfield <rjh@cpax.org.uk> - 2022-01-06 00:24 +0000
    Re: Fast randomizer Härra Ramob <ya12983@mail.com> - 2022-01-05 00:39 -0800

csiph-web