I found what seems like an interesting anomaly in JavaScript. Which focuses on my attempts to speed up the calculation of trigonometric transformations by precomputing sin (x) and cos (x) and simply referring to precomputed values.
Intuitively, one would expect that the preliminary calculation would be faster than evaluating the functions Math.sin () and Math.cos () each time. Especially if your application design will use only a limited set of values for the argument of trigger functions (in my case, integer degrees in the interval [0 °, 360 °), which is enough for my purposes here).
So, I did a little test. After preliminary calculation of the values of sin (x) and cos (x), storing them in 360-element arrays, I wrote a short test function activated by a button on a simple test HTML page to compare the speed of the two approaches. One loop simply multiplies the value by the value of the precomputed element of the array, while the other loop multiplies the value by Math.sin ().
I expected that a precomputed loop would be noticeably faster than a loop involving a function call for a trigger function. To my surprise, the precomputed cycle was slower .
Here is the test function I wrote:
function MyTest()
{
var ITERATION_COUNT = 1000000;
var angle = Math.floor(Math.random() * 360);
var test1 = 200 * sinArray[angle];
var test2 = 200 * cosArray[angle];
var ref = document.getElementById("Output1");
var outData = "Test 1 : " + test1.toString().trim() + "<br><br>";
outData += "Test 2 : "+test2.toString().trim() + "<br><br>";
var time1 = new Date();
for (var i=0; i<ITERATION_COUNT; i++)
{
var angle = Math.floor(Math.random() * 360);
var test3 = (200 * sinArray[angle]);
}
var time2 = new Date();
var msec1 = (time1.getUTCSeconds() * 1000) + time1.getUTCMilliseconds();
var msec2 = (time2.getUTCSeconds() * 1000) + time2.getUTCMilliseconds();
var elapsed1 = msec2 - msec1;
outData += "Test 3 : Elapsed time is " + elapsed1.toString().trim() + " milliseconds<br><br>";
var time1 = new Date();
for (var i=0; i<ITERATION_COUNT; i++)
{
var angle = Math.floor(Math.random() * 360);
var test3 = (200 * Math.sin((Math.PI * angle) / 180));
}
var time2 = new Date();
var msec1 = (time1.getUTCSeconds() * 1000) + time1.getUTCMilliseconds();
var msec2 = (time2.getUTCSeconds() * 1000) + time2.getUTCMilliseconds();
var elapsed2 = msec2 - msec1;
outData += "Test 4 : Elapsed time is " + elapsed2.toString().trim() + " milliseconds<br><br>";
ref.innerHTML = outData;
}
, , , , , .
( 3 - , 4 - , Math.sin()):
1:
Test 3 : Elapsed time is 153 milliseconds
Test 4 : Elapsed time is 67 milliseconds
2:
Test 3 : Elapsed time is 167 milliseconds
Test 4 : Elapsed time is 69 milliseconds
3:
Test 3 : Elapsed time is 265 milliseconds
Test 4 : Elapsed time is 107 milliseconds
4:
Test 3 : Elapsed time is 162 milliseconds
Test 4 : Elapsed time is 69 milliseconds
, , , , , ? , , ?
- , , . , (, , , !), JavaScript, , . , , , JavaScript , , , , , .
, ?
Google Chrome:
60.0.3112.101 ( ) (64- )
64- Windows 7. Firefox, , , .
, JavaScript, , !