Scripters cove
Moderators: winston, another_commander
Re: Scripters cove
No surprise to get so different results with absolutely different approaches. The snippet with forEach creates a new function on every iteration...
- Capt. Murphy
- Commodore
- Posts: 1127
- Joined: Fri Feb 25, 2011 8:46 am
- Location: UK South Coast.
Re: Scripters cove
Interesting - I did another repeat of the test but with a simple arithmetic operation to cut out the native overhead - each element was multiplied by 2. array.forEach performance is very poor compared to the various ways for loops. It looks nice in code, but maybe should be avoided.
Edit to add - thanks Svengali. Is there a situation where forEach would be the best way?
Edit to add - thanks Svengali. Is there a situation where forEach would be the best way?
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
Last edited by Capt. Murphy on Wed Dec 14, 2011 12:46 pm, edited 1 time in total.
Capt. Murphy's OXPs
External JavaScript resources - W3Schools & Mozilla Developer Network
Win 7 64bit, Intel Core i5 with HD3000 (driver rev. 8.15.10.2696 - March 2012), Oolite 1.76.1
External JavaScript resources - W3Schools & Mozilla Developer Network
Win 7 64bit, Intel Core i5 with HD3000 (driver rev. 8.15.10.2696 - March 2012), Oolite 1.76.1
Re: Scripters cove
I've digged a while back into loop optimizations. And yes, counting downwards is often faster.
http://www.websiteoptimization.com/speed/10/10-3.html
http://devpro.it/examples/loopsbench/
http://www.websiteoptimization.com/speed/10/10-3.html
http://devpro.it/examples/loopsbench/
Re: Scripters cove
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?
- Cmdr. Maegil
- Sword-toting nut-job
- Posts: 1294
- Joined: Tue Feb 27, 2007 10:28 pm
- Location: On the mend in Western Africa
Re: Scripters cove
Does anyone have an idea on how to stop/restart the player turrets at will?
Cancelling the current target or setting it to a non-hostile will stop them, but there are situations where I want to be able to use the targeting system without having them shooting at my target, and in tight melees too often the turrets end up shooting what they shouldn't.
Cancelling the current target or setting it to a non-hostile will stop them, but there are situations where I want to be able to use the targeting system without having them shooting at my target, and in tight melees too often the turrets end up shooting what they shouldn't.
You know those who, having been mugged and stabbed, fired, dog run over, house burned down, wife eloped with best friend, daughters becoming prostitutes and their countries invaded - still say that "all is well"?
I'm obviously not one of them.
I'm obviously not one of them.
Re: Scripters cove
The only way is to physically remove them (remove them as sub-entities), and to restore them when you want to turn them on again. But there is the additional item that if you dock with missing sub-ents, then you'll get offered a maintenance overhaul (at a large cost usually) to restore them as the game thinks they've been shot off rather than removed.
It can be worked around though, with a bit of scripting. Look at the script for the Vortex - that has switchable turrets by this method (including the work-around for the overhaul issue).
It can be worked around though, with a bit of scripting. Look at the script for the Vortex - that has switchable turrets by this method (including the work-around for the overhaul issue).
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- Commander McLane
- ---- E L I T E ----
- Posts: 9520
- Joined: Thu Dec 14, 2006 9:08 am
- Location: a Hacker Outpost in a moderately remote area
- Contact:
Re: Scripters cove
And it's only workarounds because turrets were originally intended as non-player equipment only.
- Eric Walch
- Slightly Grand Rear Admiral
- Posts: 5536
- Joined: Sat Jun 16, 2007 3:48 pm
- Location: Netherlands
Re: Scripters cove
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.
UPS-Courier & DeepSpacePirates & others at the box and some older versions
- Smivs
- Retired Assassin
- Posts: 8408
- Joined: Tue Feb 09, 2010 11:31 am
- Location: Lost in space
- Contact:
Re: Scripters cove
And so they should have remained!Commander McLane wrote:because turrets were originally intended as non-player equipment only.
Mind you, the WonderWorm wouldn't be half as silly without them, so......
Commander Smivs, the friendliest Gourd this side of Riedquat.
Re: Scripters cove
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.
If not then yes, just activating weapons-safe will work.
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- Wildeblood
- ---- E L I T E ----
- Posts: 2453
- Joined: Sat Jun 11, 2011 6:07 am
- Location: Western Australia
- Contact:
Re: Scripters cove
Is there a way to suppress the "Target lost." console message when setting player.ship.target = null or removing the targeted entity? This is the code I have been using:-
Later, that same evening...
I want to remove the drone quietly without drawing attention to it, but regardless of whether I set player.ship.target = null first, or just remove it, it always causes a "Target lost." console message.
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;
}
- Commander McLane
- ---- E L I T E ----
- Posts: 9520
- Joined: Thu Dec 14, 2006 9:08 am
- Location: a Hacker Outpost in a moderately remote area
- Contact:
Re: Scripters cove
You can of course remove the drone quietly if you don't make it the player's target in the first place. But I guess making it the target is essential for what you want to do, so this may not be an option.
- Cmdr. Maegil
- Sword-toting nut-job
- Posts: 1294
- Joined: Tue Feb 27, 2007 10:28 pm
- Location: On the mend in Western Africa
Re: Scripters cove
<shameless plug>...of course, if you had handlers over the controls you could order the code to "press r" or flick the weapons off, then back on, to cancel the target lock-on just before removing it...</shameless plug>
You know those who, having been mugged and stabbed, fired, dog run over, house burned down, wife eloped with best friend, daughters becoming prostitutes and their countries invaded - still say that "all is well"?
I'm obviously not one of them.
I'm obviously not one of them.
Re: Scripters cove
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.
- Commander McLane
- ---- E L I T E ----
- Posts: 9520
- Joined: Thu Dec 14, 2006 9:08 am
- Location: a Hacker Outpost in a moderately remote area
- Contact:
Re: Scripters cove
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.