Re: Scripters cove
Posted: Wed Dec 14, 2011 12:36 pm
No surprise to get so different results with absolutely different approaches. The snippet with forEach creates a new function on every iteration...
For information and discussion about Oolite.
https://bb.oolite.space/
Code: Select all
this.forwardforloop = function()
{
var counter = 0;
this.arrayOfSystemNames = new Array;
for (counter = 0; counter < this.arrayOfSystemID.length;counter++)
{
this.arrayOfSystemNames.push(this.arrayOfSystemID[counter]*2)
}
}
Total time: 0.934 ms
JavaScript: 0.764 ms, native: 0.159 ms
Counted towards limit: 0.912 ms, excluded: 0.0219997 ms
Profiler overhead: 0.153 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:19) <anonymous> J 1 0.65 0.65 69.5 69.5 0.65
WorldScriptsGetProperty N 1 0.16 0.15 17.0 16.3 0.15
(<console input>) codeToBeProfiled J 1 0.92 0.12 98.8 12.3 0.12
OOStringFromJSString N 1 0.01 0.01 0.7 0.7 0.01
Code: Select all
this.optimisedforwardforloop = function()
{
var counter = 0;
var length = this.arrayOfSystemID.length
this.arrayOfSystemNames = new Array;
for (counter = 0; counter < length ;counter++)
{
this.arrayOfSystemNames.push(this.arrayOfSystemID[counter]*2)
}
}
Total time: 0.841 ms
JavaScript: 0.67 ms, native: 0.159 ms
Counted towards limit: 0.818 ms, excluded: 0.0229999 ms
Profiler overhead: 0.125 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:29) <anonymous> J 1 0.58 0.58 69.4 69.4 0.58
WorldScriptsGetProperty N 1 0.16 0.15 18.9 18.1 0.15
(<console input>) codeToBeProfiled J 1 0.83 0.09 98.6 10.2 0.09
OOStringFromJSString N 1 0.01 0.01 0.8 0.8 0.01
Code: Select all
this.reversedforloop = function()
{
var counter;
this.arrayOfSystemNames = new Array;
for (counter = this.arrayOfSystemID.length - 1; counter >= 0;counter--)
{
this.arrayOfSystemNames.push(this.arrayOfSystemID[counter]*2)
}
}
Total time: 0.816 ms
JavaScript: 0.638 ms, native: 0.167 ms
Counted towards limit: 0.794 ms, excluded: 0.0219997 ms
Profiler overhead: 0.121 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:40) <anonymous> J 1 0.56 0.56 68.1 68.1 0.56
WorldScriptsGetProperty N 1 0.17 0.16 20.5 19.6 0.16
(<console input>) codeToBeProfiled J 1 0.80 0.08 98.7 10.0 0.08
OOStringFromJSString N 1 0.01 0.01 0.9 0.9 0.01
Code: Select all
this.optimisedreversedforloop = function()
{
var counter;
var length = this.arrayOfSystemID.length - 1;
this.arrayOfSystemNames = new Array;
for (counter = length; counter >= 0;counter--)
{
this.arrayOfSystemNames.push(this.arrayOfSystemID[counter]*2)
}
}
Total time: 0.717 ms
JavaScript: 0.544 ms, native: 0.159 ms
Counted towards limit: 0.695 ms, excluded: 0.0219997 ms
Profiler overhead: 0.12 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:50) <anonymous> J 1 0.27 0.27 38.2 38.2 0.27
(<console input>) codeToBeProfiled J 1 0.70 0.27 98.0 37.7 0.27
WorldScriptsGetProperty N 1 0.16 0.15 22.2 21.2 0.15
OOStringFromJSString N 1 0.01 0.01 1.0 1.0 0.01
Code: Select all
this.forEachloop = function()
{
this.arrayOfSystemNames = this.arrayOfSystemID.forEach(function(systemID){return systemID*2},this);
}
Total time: 7.923 ms
JavaScript: 7.753 ms, native: 0.159 ms
Counted towards limit: 7.901 ms, excluded: 0.0219997 ms
Profiler overhead: 4.858 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:61) <anonymous> J 512 6.99 5.40 88.2 68.1 0.24
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:61) <anonymous> J 1 7.67 2.28 96.9 28.8 2.28
WorldScriptsGetProperty N 1 0.16 0.15 2.0 1.9 0.15
(<console input>) codeToBeProfiled J 1 7.91 0.08 99.9 1.0 0.08
OOStringFromJSString N 1 0.01 0.01 0.1 0.1 0.01
If I understand it right there is no general advice. Every loop has different factors and it's up to the scripter to choose the right way for a specific situation. Even loop unrolling can be pretty slow compared to other ways.Capt. Murphy wrote:Edit to add - thanks Svengali. Is there a situation where forEach would be the best way?
I thought we had a key for this: weapons off line, (the '_' key)Cmdr. Maegil wrote:Does anyone have an idea on how to stop/restart the player turrets at will.
And so they should have remained!Commander McLane wrote:because turrets were originally intended as non-player equipment only.
I got the impression from his post that he wanted to keep his lasers active, just switch off the turrets.Eric Walch wrote:I thought we had a key for this: weapons off line, (the '_' key)Cmdr. Maegil wrote:Does anyone have an idea on how to stop/restart the player turrets at will.
Code: Select all
this.my_drone = system.addShips("splinter", 1, player.ship.position.add(player.ship.heading.multiply(5000)), 5000)[0];
player.ship.target = this.my_drone;
Code: Select all
if (this.my_drone)
{
if (player.ship.target === this.my_drone) player.ship.target = null;
this.my_drone.remove(true);
// delete this.my_drone;
}
Not to my knowledge. The manifest object only exists for the player ship, as far as I know.cim wrote:Quick question: is there a way with script to find out how much cargo a station has in its market, and/or at what price? I can't see one in the docs, but I want to check I'm not missing something obvious.