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?