-
Notifications
You must be signed in to change notification settings - Fork 735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Coding GuildLines (discussion) #2120
Comments
Although it has to be noted, that for most smaller switch cases the difference is not as big. Another big thing to consider is to fail as early as possible, to not waste any calculation time where you already know it's not going to matter. Instances of that are chained boolean evaluations as return values, that could be separated out into multiple This also counts for parameter and fail state evaluation at the beginning of a file. So a small example would be something like #include "script_component.hpp"
#define SOMESTATICVALUE 5
if (isServer) exitWith {};
private ["_someVariable"];
params ["_that", "_thing"];
if (isNull _thing) exitWith {};
_that = _that + 5;
_someVariable = _thing;
if (_that < SOMESTATICVALUE) exitWith {false};
if (ACE_player != _thing) exitWith {false};
true |
Guys, there comes a point where the performance gain is totally negligible and changes only make the code less readable. 😛 Moving conditions before the private statement is not something I'd bother to do. Also, you are aware that Also there's some redundancy in your example:
|
yes its slower than then faster but than Swtich Case |
@SilentSpike I guess I wasn't clear enough with this:
What I meant here is somethings along the lines of ACE3/addons/respawn/functions/fnc_canMoveRallypoint.sqf Lines 27 to 33 in 3a3dad9
where the gain of exiting early is a big benefit. (Obviously there are other optimizations, that can get rid of this specific example, but that's what came to mind right away.) |
One thing that really should be considered is how often the code is run. If something is inside of a loop or a per frame or called on every shot fired, then yes we need to optimize it as best we can. But for code that is run ~5 times in a session we don't need to go too far. Readability and maintenance can become just as important. |
@PabstMirror absolutely agreed. |
I agree with @PabstMirror here as well, additional case where speed can be very important besides loops is where a code must execute very quickly, eg. something doesn't interrupt it or similar. |
There are more differences between
The difference might be 20% for very basic code, but even that is neglible when the content of the loop gets more complex. Both commands are reasonably fast (for SQF), so 20% isn't actually that much of an improvement in time. I wouldn't replace |
hmm you can add a boolean or nil at the end and dont have problem with a pushback and save performance |
I kinda agree. 'Cause we all know the saying: Maybe someone could do performance tests to see if it is really that much of a difference. |
yeah ruthberg and i tested it both and count is on our both maschines ca 20% faster |
I would test it, but game updater ate my arma3.exe and the steam check files shit is still at 0% after an hour. |
I just ran some tests for you to get you some values. I filled an array with 1000 string elements
and
This is to minimize the effect of any outside performance influence to a point where the difference is more clear. The performance tests where conducted using CBA's
Tests were conducted in the editor, with an empty map except 1 unit, running the development version of the current master branch. Here is a couple of values I observed:
Obviously this test is designed to make the difference extremely clear if there is any. Actual performance improvements will vary, depending on the implementation they are used in. However it is clear that there is performance improvements. Result values are not always relevant when it comes to iterations. If there is the rare case of it actually mattering, it would likely be an easy fix. When it comes to readability, both are fairly equivalent in my personal opinion. |
In a real application of The time you'd use on replacing |
@commy2 since we're already going over the code anyways, this is a good opportunity to actually replace those. Even if it's just a small gain. There is other aspects, that slowly are getting replaced, for example the deprecation of the TL;DR: We're doing code cleanups anyways, why not take advantage of that minor bit of performance, introduce it as something that is encouraged to do, but not enforced. |
Go ahead. Just remember to make sure to return nil. Don't expect anything noticable though. |
Ah. And test if |
Thinking about it logically, in the EDIT: I'll test that in a bit. |
Yes, you are correct. Edit: |
Replacing |
Re: Explain the difference in the documentation, but only enforce it in old code that hasn't changed in a while. Coders constantly tweak their stuff, especially if it's an initial release. It would be bad if PRs get stuck in gridlock because the code doesn't follow a minor performance benefit. |
I don't see the count vs foreach becoming a standard. Best practice, I'd say. I do disagree with the statement made by @jonpas; I think count does reduce readability as it's not immedialy clear what the intend of the code is. Do we want to count something or just simply loop through some elements? Edit: that's not to say I don't think we should replace forEach by count where performance matters a lot; For example in per frame handlers. |
I can relate with glowbal's comment. The reason I barely ever use count over forEach is because when I come back to the code in 2 hours/days/weeks/months/years time, forEach is understandable instantly: i'm doing something for each of these things, whereas count doesn't come so fast. |
Mike and i are currently Updating and Fixing all ACE Module Scripts and we are change some code for better performance and Readabilty they are not include in the Coding Guildlines for Example:
we Currently Change the PARAMS_N Macro from CBA to the params command that is now hardCoded in ArmA 3 sinse Update 1.48.
and we change some ForEach to Count because a Count is ca. 20% faster than a forEach but dont have a _forEachIndex
Other Changes are we doing for Swtiche Cases the Reason is that a Switch case is slower than a call {if ()exitWith{};}; and in only some places act different from a switch
The text was updated successfully, but these errors were encountered: