AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Another efficiency questoin (https://forums.alliedmods.net/showthread.php?t=192307)

Liverwiz 08-08-2012 19:06

Another efficiency questoin
 
This is probably silly. But which is faster. Checking, then setting if it needs to be. Or just setting the value to what it needs to be. See following code examples.

PHP Code:

for(new 1<= g_maxClientsi++)
        
blockList[i][id] = false    // Unblock the client, if left blocked 

or

PHP Code:

or(new 1<= g_maxClientsi++)
{
    if(
blockList[i][id])
        
blockList[i][id] = false    // Unblock the client, if left blocked


My guess is the first, but i'm also curious to know for sure.
It should also be noted that it is most likely to be false anyway.

Bugsy 08-08-2012 19:08

Re: Another efficiency questoin
 
First one .. or use bit-fields and just do var = 0 (no loop needed) .. or use arrayset

Liverwiz 08-08-2012 20:00

Re: Another efficiency questoin
 
Quote:

Originally Posted by Bugsy (Post 1767315)
First one .. or use bit-fields and just do var = 0 (no loop needed) .. or use arrayset

Dewd! Thanks for that arrayset native! I'm using that.

As for using bit-fields. could you show me an example that would be faster than arrayset? Mostly because i do want to be able to use bitsums more, i just don't feel adequately comfortable with them.

Bugsy 08-08-2012 20:03

Re: Another efficiency questoin
 
Here's a good read to get started.

Firstly, they can only be used for booleans. In a single 32-bit cell you have 32 booleans. You can easily stretch it beyond that using an array and some shifting trickery. Read through the tut I posted, it covers most of what you'll need to know.

Edit: Let me re-phrase, you have 32 booleans to work with in a single cell. You can also store other values but you're limited to the number of bits needed to represent the value.

ie.
32 numbers ranging from 0-1
16 numbers ranging from 0-3
8 numbers ranging from 0-15
4 numbers ranging from 0-255
2 numbers ranging from 0-65535

Liverwiz 08-08-2012 20:08

Re: Another efficiency questoin
 
Thanks man!

As for arrayset....it looks like it doesn't work with a matrix (which is required for this) so i guess i'm going to have to go back to loop till i figure out a clever way of using bits.

Bugsy 08-08-2012 20:16

Re: Another efficiency questoin
 
Multi-dimension array?
PHP Code:

new arrTest32 ];
arraysetarrTest sizeofarrTest ) );
    
new 
arrTest232 ][ 32 ];
arraysetarrTest2[5] , sizeofarrTest2[] ) );
    
new 
arrTest332 ][ 32 ][ 32 ];
arraysetarrTest3[5][6] , sizeofarrTest3[][] ) ); 


Liverwiz 08-08-2012 20:25

Re: Another efficiency questoin
 
I don't quite understand that example....how would i do it setting all cells in x column? matrix[-set-][X]
In other words....how do i apply it to my above loop?

Trying it out these do not compile...
Code:

arrayset(blockList[id][], false, sizeof(blockList[id]) )
arrayset(blockList[][id], false, sizeof(blockList[]) )
arrayset(blockList[][id], false, 33)

all gives me this...
Error: Invalid expression, assumed zero on line 127
line 127 being the arrayset line.


EDIT: *facepalm* arrayset(blockList[id], false, 33 ) works....

fysiks 08-08-2012 20:34

Re: Another efficiency questoin
 
You should never check a variable's value if you are going to set it to the same value regardless anyways. You are actually increasing the number of times memory is accessed.

Bugsy 08-09-2012 08:54

Re: Another efficiency questoin
 
My fault liverwiz, my code was partially wrong, fixed. Use sizeof.

Liverwiz 08-09-2012 18:31

Re: Another efficiency questoin
 
Quote:

Originally Posted by Bugsy (Post 1767598)
My fault liverwiz, my code was partially wrong, fixed. Use sizeof.

Oh. that makes more sense.

now....question. What if i use g_maxClients instead of sizeof? Would the difference in iterations be irrelevant? Would it simply not work? Or am i just being silly?
I ask because its kinda hard to fill a test server to try out the last spot.


All times are GMT -4. The time now is 05:51.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.