Raised This Month: $ Target: $400
 0% 

Another efficiency questoin


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 08-08-2012 , 19:06   Another efficiency questoin
Reply With Quote #1

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.
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-08-2012 , 19:08   Re: Another efficiency questoin
Reply With Quote #2

First one .. or use bit-fields and just do var = 0 (no loop needed) .. or use arrayset
__________________

Last edited by Bugsy; 08-08-2012 at 19:16.
Bugsy is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 08-08-2012 , 20:00   Re: Another efficiency questoin
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
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.
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-08-2012 , 20:03   Re: Another efficiency questoin
Reply With Quote #4

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
__________________

Last edited by Bugsy; 08-08-2012 at 20:11.
Bugsy is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 08-08-2012 , 20:08   Re: Another efficiency questoin
Reply With Quote #5

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.
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-08-2012 , 20:16   Re: Another efficiency questoin
Reply With Quote #6

Multi-dimension array?
PHP Code:
new arrTest32 ];
arraysetarrTest sizeofarrTest ) );
    
new 
arrTest232 ][ 32 ];
arraysetarrTest2[5] , sizeofarrTest2[] ) );
    
new 
arrTest332 ][ 32 ][ 32 ];
arraysetarrTest3[5][6] , sizeofarrTest3[][] ) ); 
__________________

Last edited by Bugsy; 08-09-2012 at 08:53.
Bugsy is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 08-08-2012 , 20:25   Re: Another efficiency questoin
Reply With Quote #7

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....
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 08-08-2012 at 20:26.
Liverwiz is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-08-2012 , 20:34   Re: Another efficiency questoin
Reply With Quote #8

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.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-09-2012 , 08:54   Re: Another efficiency questoin
Reply With Quote #9

My fault liverwiz, my code was partially wrong, fixed. Use sizeof.
__________________
Bugsy is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 08-09-2012 , 18:31   Re: Another efficiency questoin
Reply With Quote #10

Quote:
Originally Posted by Bugsy View Post
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.
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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