Page 36 of 118

Re: Scripters cove

Posted: Wed Dec 14, 2011 12:36 pm
by Svengali
No surprise to get so different results with absolutely different approaches. The snippet with forEach creates a new function on every iteration...

Re: Scripters cove

Posted: Wed Dec 14, 2011 12:37 pm
by Capt. Murphy
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?

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

Re: Scripters cove

Posted: Wed Dec 14, 2011 12:43 pm
by Svengali
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/

Re: Scripters cove

Posted: Wed Dec 14, 2011 12:56 pm
by Svengali
Capt. Murphy wrote:
Edit to add - thanks Svengali. Is there a situation where forEach would be the best way?
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.

Re: Scripters cove

Posted: Sun Jan 08, 2012 1:17 pm
by Cmdr. Maegil
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.

Re: Scripters cove

Posted: Sun Jan 08, 2012 1:37 pm
by Thargoid
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).

Re: Scripters cove

Posted: Sun Jan 08, 2012 4:49 pm
by Commander McLane
And it's only workarounds because turrets were originally intended as non-player equipment only.

Re: Scripters cove

Posted: Sun Jan 08, 2012 5:03 pm
by Eric Walch
Cmdr. Maegil wrote:
Does anyone have an idea on how to stop/restart the player turrets at will.
I thought we had a key for this: weapons off line, (the '_' key)

Re: Scripters cove

Posted: Sun Jan 08, 2012 5:09 pm
by Smivs
Commander McLane wrote:
because turrets were originally intended as non-player equipment only.
And so they should have remained!
Mind you, the WonderWorm wouldn't be half as silly without them, so...... :P

Re: Scripters cove

Posted: Sun Jan 08, 2012 5:18 pm
by Thargoid
Eric Walch wrote:
Cmdr. Maegil wrote:
Does anyone have an idea on how to stop/restart the player turrets at will.
I thought we had a key for this: weapons off line, (the '_' key)
I got the impression from his post that he wanted to keep his lasers active, just switch off the turrets.

If not then yes, just activating weapons-safe will work.

Re: Scripters cove

Posted: Tue Jan 17, 2012 5:34 pm
by Wildeblood
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:-

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;
Later, that same evening...

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;
			}
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.

Re: Scripters cove

Posted: Tue Jan 17, 2012 11:06 pm
by Commander McLane
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.

Re: Scripters cove

Posted: Tue Jan 17, 2012 11:17 pm
by Cmdr. Maegil
<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>

Re: Scripters cove

Posted: Thu Jan 19, 2012 6:13 pm
by cim
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.

Re: Scripters cove

Posted: Thu Jan 19, 2012 10:26 pm
by Commander McLane
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.
Not to my knowledge. The manifest object only exists for the player ship, as far as I know.