Quote:
Originally Posted by Krystek.
Is this execution correct?
PHP Code:
new damage_better_items = random_num(3, 12); new damage_items = random_num(1, 6);
new perks_id[] = { 1, 2, 3, 4, 7, 13 };
for( new i; i < sizeof(perks_id); i++ ) { if( cod_get_user_perk(vid) == perks_id[i] ) { log_amx("Better Durability: %i | PerkID %i", durability_perks[vid], perks_id[i]); durability_perks[vid] -= (durability_perks[vid] > damage_better_items)? damage_better_items: durability_perks[vid]; log_amx("Better Durability: %i | PerkID %i", durability_perks[vid], perks_id[i]);
return PLUGIN_CONTINUE; } }
for( new i; i < sizeof(perks_id); i++ ) { if( cod_get_user_perk(vid) != perks_id[i] ) { log_amx("Durability: %i", durability_perks[vid], perks_id[i]); durability_perks[vid] -= (durability_perks[vid] > damage_items)? damage_items: durability_perks[vid]; log_amx("Durability: %i", durability_perks[vid], perks_id[i]);
return PLUGIN_CONTINUE; } }
The code is pretty loose so don't worry about log_amx and the like.
|
The thing that stands out to me about this is that you have code execution for all every cell in perks_id[] (because the if statements are opposite so one or the other code blocks will get executed) but you're being inefficient about it by using two loops.
You can do this in a single loop by just looping through all cells of the array and just using an if-then-else to determine which code you execute on each iteration.
P.S. While I understand that it's only part of your debugging, it's probably a good thing to point out so that you get better at seeing your coding mistakes in general: your log_amx() functions in your second loop should be throwing errors because you pass two arguments but the format string only has one.
_____________________________________________ _____
Quote:
Originally Posted by damage220
I am sorry for the first advice. Though I knew that sizeof in C is calculated at compile time, I had no luck to find any confirmation regarding PAWN.
|
I too was looking in the Pawn Language Guide to see if I could find it stated as such but I could not. This is why I was less absolute about my words on this one.
Quote:
Originally Posted by damage220
Why did you decide that the function is not called very often? In fact I do not see any function at all, just the code. There is no function signature or event hook declared.
|
My advice was not 100% based on the OP's code because I was primarily correcting what you said so that people don't accidentally use it.
You mentioned moving a variable to the global scope as an optimization. While that would potentially optimize it (depending on the specific code) it's not the only way to do it. The reason you said to move it to the global scope was so that it didn't have to dynamically re-allocate the memory each time the function is called. Well, you can do this same thing while keeping it in the function scope by declaring it as static.
Regarding if a function (or rather a block of code) is called often, that's because of the dynamic allocation functionality. If it's called often, it has to get re-allocated a lot. If it's big then this re-allocation becomes more expensive. So, making it static (or global) removes the re-allocation and thus reduce processor time required. For small variables or code that is rarely called, the processor time required stays relatively low so it's not really worth optimizing it (i.e. it's a negligible performance cost).
Quote:
Originally Posted by damage220
If a variable is used only in one function, I agree it should be declared in that function.
If a constant is never changes, like PI, it can be declared in a function.
But when a constant is subject to change I would prefer to have it global, just for convenience for future edits. I was kind of thinking that perks_id is subject to change, and so I would move it out of the function to simplify future edits and to reduce the number of memory allocations.
|
Whether or not it's a constant (constants don't change) or a variable does not dictate in what scope it should be declared. You should declare the constant or variable in the proper scope for it's use case. If a constant or variable is only useful to the code within a specific function, it should be declared in the function.
Whether or not the perks_id array will change is not clear in the code posted by the OP. If it's within a function and is not used outside the function, it makes sense to declare it inside the function. If the OP decides to change how perks_id is used, the best scope for that variable can be re-evaluated.
Granted, you can declare everything globally if you really wanted to as long as you don't have naming conflicts with any other variable in the entire code. However, this can make the code unnecessarily complex. Scoping variables helps makes code simpler and more modular.
_____________________________________
We probably don't need to go any further with this part of the discussion. We don't want to hijack the thread any more
.
__________________