Code Optimisation

From Bohemia Interactive Community
Jump to navigation Jump to search

Make it work.

No need to worry about making it work at light speed if it doesn't even do what it is supposed to. Focus on getting a working product first.

Make it fast.

Optimisation is everything when running lots of instances, with low delays. However, there is such thing as premature optimisation. Also, avoid excessive cleverness.

"Excessive cleverness is doing something in a really clever way when actually you could have done it in a much more straightforward but slightly less optimal manner. You've probably seen examples of people who construct amazing chains of macros (in C) or bizarre overloading patterns (in C++) which work fine but which you look at an go "wtf"? EC is a variation of premature-optimisation. It's also an act of hubris - programmers doing things because they want to show how clever they are rather than getting the job done." - sbsmac

Preprocessfilelinenumbers

This command remembers what it has done, so loading a file once will load it into memory, therefore if wanted to refrain from using global variables for example, but wanted a function precompiled, but not saved, you could simply use: call compile preprocessfilelinenumbers "file"

Remembering the only loss of performance will be the compile time of the string returned and then the call of the code itself.

Make it pretty.

Documentation, readability, and all that jazz. Clean code is good code.

Written it twice? Put it in a function

Pre-compilation by the game engine can save up 20x the amount of time processing, even if the initial time is slightly lengthened. If you've written it twice, or if there is a kind of loop consistently being compiled (perhaps a script run by execVM), make it into a function (FUNCVAR =compile preprocessfilelinenumbers "filename.sqf").

Length

If any script or function is longer than around 200-300 lines, then perhaps (not true in all cases by all means) you may need to rethink the structure of the script itself, and whether it is all within scope of the functionality required, and if you could do something cleaner, faster and better.

If Else If Else If Else ...

If you can't escape this using a switch control structure, then try and rethink the functionality. Especially if only one option is needed to match.

Constants

Using a hard coded constant more than once? Use preprocessor directives rather than storing it in memory. Such as: a = _x + 1.053; b = _y + 1.053;

Becomes:

  1. define BUFFER 1.053

_a = _x + BUFFER; _b = _y + BUFFER; This also allows quick modifying of code.


Adding elements to an array

Removing elements from an array

When FIFO removing elements from an array, the set removal method works best, even if it makes a copy of the new array.

ARRAYX set [0, objnull]; ARRAYX = ARRAYX - [objnull];

Getting position

If your feeling self-conscious and want an AGL solution (ie identical to that of getPos): _pos=getposATL player; if(surfaceIsWater _pos)then{_pos = getposASL player};

It is still 25% faster than its getPos twin.

CreateVehicle Array

http://community.bistudio.com/wiki/createVehicle

Does not take 3D positions, 2D only, and is 500x slower than its younger brother:

http://community.bistudio.com/wiki/createVehicle_array

You know what to do. (Incase you don't, USE CREATEVEHICLE ARRAY!!!)

10,000 Iterations Limit in Loops

Doesn't exist in scheduled environments, only in non - scheduled (only where the 0.3ms delay does not exist).

Loops

These first two loop types are identical in speed (+/- 10%), and are more than 3x as fast the proceeding two loop types.

  • for "_y" from # to # step # do { ... };
  • { ... } foreach [ ... ];

Where as these two loops are much slower, and for maximum performance, avoided.

  • while { expression } do { code };
  • for [{ ... },{ ... },{ ... }] do { ... }

Waituntil can be used when you want something to only run once per frame, which can be handy for limiting scripts that may be resource heavy.

  • waituntil {expression};

As requested, the method to gain this information was via the CBA_fnc_benchmarkFunction, using around 10000 iterations. It was not tested across different stations, and *may* be subject to change between them (ArmA2 is special remember :P):

fA = { private "_i"; _i = 0; while {_i < 1000} do { _i = _i + 1; private "_t"; _t = "0"; }; };

fB = { for "_i" from 0 to 1000 do { private "_t"; _t = "0" }; };

This code then performs 10,0000 tests and returns average time taken for the function, measured via diag_ticktime. [fA,[],10000] call CBA_fnc_benchmarkFunction; [fB,[],10000] call CBA_fnc_benchmarkFunction;

Avoid O(n^2)!!

Commonly you may set up foreach foreach's. 'For' example:

  • { { ... } foreach [0,0,0]; } foreach [0,0,0];

This example is of the order (n^2) (3^2 = 9 iterations). For arrays that are twice as big, you will run 4 times slower, and for arrays that are 3 times as big you will run 9 times slower! Of course, you don't always have a choice, and if one (or both) of the arrays is guaranteed to be small it's not really as big of a deal.

Avoiding the 0.3ms delay

Many times you may want to avoid the 0.3 ms delay (not 3ms!), and there a few tricks around how to do it...

  • Triggers are inside what we call the 'non-scheduled' environment.
  • All pre-init code executions are without scheduling.
  • FSM conditions are without scheduling
  • Event handlers (on units and in GUI) are without scheduling

Measuring Velocity Scalar

Sure we can just use Pythagorean theorem to calculate the magnitude from a velocity vector, but a command native to the engine runs much faster (over 10x faster) than the math.

  • VECTOR distance [0,0,0]

Works for 2D vectors as well.

How to test and gain this information yourself?

There is a few ways to measure the information and run time durations inside ArmA2, mostly using differencing of the time itself. The CBA package includes a function for you to test yourself, however if you are remaining ad don free or cannot use this, the following code setup is as effective; and allows different ways to retrieve the information (chat text, rpt file, clipboard)

_fnc_dump = { player globalchat str _this; diag_log str _this; //copytoclipboard str _this; }; _t1 = diag_tickTime; // ... code to test (diag_tickTime - _t1) call _fnc_dump;