Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Variables take 7x longer to access than global variables?

I was trying to benchmark the gain/loss of "caching" math.floor, in hopes that I could make calls faster.

Here was the test:

<html>
<head>
<script>
window.onload = function()
{
  var startTime = new Date().getTime();
  var k = 0;
  for(var i = 0; i < 1000000; i++) k += Math.floor(9.99);
  var mathFloorTime = new Date().getTime() - startTime;

  startTime = new Date().getTime();
  window.mfloor = Math.floor;
  k = 0;
  for(var i = 0; i < 1000000; i++) k += window.mfloor(9.99);
  var globalFloorTime = new Date().getTime() - startTime;

  startTime = new Date().getTime();
  var mfloor = Math.floor;
  k = 0;
  for(var i = 0; i < 1000000; i++) k += mfloor(9.99);
  var localFloorTime = new Date().getTime() - startTime;

  document.getElementById("MathResult").innerHTML = mathFloorTime;
  document.getElementById("globalResult").innerHTML = globalFloorTime;
  document.getElementById("localResult").innerHTML = localFloorTime;
};
</script>
</head>
<body>
Math.floor: <span id="MathResult"></span>ms <br />
var mathfloor: <span id="globalResult"></span>ms <br />
window.mathfloor: <span id="localResult"></span>ms <br />
</body>
</html>

My results from the test:

[Chromium 5.0.308.0]:  
Math.floor: 49ms  
var mathfloor: 271ms  
window.mathfloor: 40ms  

[IE 8.0.6001.18702]  
Math.floor: 703ms  
var mathfloor: 9890ms  [LOL!]  
window.mathfloor: 375ms   

[Firefox [Minefield] 3.7a4pre]
Math.floor: 42ms  
var mathfloor: 2257ms  
window.mathfloor: 60ms   

[Safari 4.0.4[531.21.10] ]
Math.floor: 92ms 
var mathfloor: 289ms 
window.mathfloor: 90ms 

[Opera 10.10 build 1893]
Math.floor: 500ms 
var mathfloor: 843ms 
window.mathfloor: 360ms

[Konqueror 4.3.90 [KDE 4.3.90 [KDE 4.4 RC1]]]
Math.floor: 453ms 
var mathfloor: 563ms 
window.mathfloor: 312ms 

The variance is random, of course, but for the most part

In all cases [this shows time taken]:
[takes longer] mathfloor > Math.floor > window.mathfloor [is faster]

Why is this? In my projects i've been using var mfloor = Math.floor, and according to my not-so-amazing benchmarks, my efforts to "optimize" actually slowed down the script by ALOT...

Is there any other way to make my code more "efficient"...? I'm at the stage where i basically need to optimize, so no, this isn't "premature optimization"...

like image 262
Warty Avatar asked Apr 20 '10 21:04

Warty


People also ask

Are local variables slower than global variables?

Conclusion: In most cases, local variables will be faster than global variables.

Are global variables faster than local variables?

Moreover, the access to local variables is faster than to global ones.

Is it better to use local or global variables?

It all depends on the scope of the variable. If you feel that a certain variable will take multiple values by passing through various functions then use local variables and pass them in function calls. If you feel that a certain variable you need to use will have constant value, then declare it as a global variable.

Do global variables take more time?

Global variables are really slow, in addition to all the other reasons not to use them.


1 Answers

You have these two variables labelled incorrectly:

var mathfloor: <span id="globalResult"></span>ms <br />
window.mathfloor: <span id="localResult"></span>ms <br />

@David's alternatives are worth looking into, as would some kind of memoisation.

like image 88
wombleton Avatar answered Nov 09 '22 16:11

wombleton