Jump to content
jazzraill

Less Code & More Variables VS More Code & Less Variables

Recommended Posts

Greetings, I'm currently working on a code that runs for each spawned entity on the map so it should be the quickest. However I'm stuck between two desicions:
First one uses less lines of code, but more private variables :
 

switch _cond1 do
	{
	case 1: 
		{
		_y = 1;_z = 3;
		};
	case 2: 
		{
		_y = 2;_z = 4;
		};
	};
switch _cond2 do
	{
	case 1: 
		{
		_c = 1;
		};
	case 2: 
		{
		_c = 3;
		};
	};
_n = (_y*_z)+_c;

The second one uses more lines of code, but no variables at all:
 

_cond1,_cond2 call
	{
	if (_cond1 isEqualTo 1) exitWith
		{
		_cond2 call
			{
			if (_cond2 isEqualTo 1) exitWith {4};
			};
		_cond2) call
			{
			if (_cond2 isEqualTo 2) exitWith {6};
			};
		};
	};
_cond1,_cond2 call
	{
	if (_cond1 isEqualTo 2) exitWith
		{
		_cond2 call
			{
			if (_cond2 isEqualTo 1) exitWith {7};
			};
		_cond2 call
			{
			if (_cond2 isEqualTo 2) exitWith {9};
			};
		};
	};

 

Share this post


Link to post
Share on other sites
9 hours ago, jazzraill said:

thanks for the reply! for anyone else wondering, the second code was almos 1.5 times faster .. (I'm also coding this on a core 2 duo so results may differ) 

 

You'll fine some useful information here https://community.bistudio.com/wiki/Code_Optimisation#How_to_test_and_gain_this_information_yourself.3F

Share this post


Link to post
Share on other sites
19 hours ago, jazzraill said:

thanks for the reply! for anyone else wondering, the second code was almos 1.5 times faster .. (I'm also coding this on a core 2 duo so results may differ) 

 

interesting, code 2 contains a bunch of errors which should prevent it from compiling let alone running

Share this post


Link to post
Share on other sites

What KK said, got some syntax errors in there.

if then / if exitWith will always be faster than switch. exitWith in combination with call is a bit faster.

 

Cheers

Share this post


Link to post
Share on other sites

The "no variable at all" is just excessive. But the code has no sense and the comparison is about some calculations vs some fixed values. No benchmark needed for that.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×