AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [INFO] Bitsums and Operators (https://forums.alliedmods.net/showthread.php?t=90507)

joaquimandrade 04-20-2009 04:41

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Empowers (Post 809875)
This A result from your code, I profiled it.. Read more carefully my post ;)

... Guess where there is a continue and where there are 10 function calls.

Empowers 04-20-2009 04:44

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by joaquimandrade (Post 809882)
... Guess where there is a continue and where there are 10 function calls.

Quote:

Originally Posted by Empowers (Post 809456)
I profiled your code here is the result:
Code:

date: Sun Apr 19 19:21:59 2009 map: de_dust
type |                            name |      calls | time / min / max
-------------------------------------------------------------------
  n |                      server_cmd |          1 | 0.000005 / 0.000005 / 0.000005
  p |                      plugin_cfg |          1 | 0.011147 / 0.011147 / 0.011147
  f |                            Index |      20000 | 0.006789 / 0.000000 / 0.000292
  f |                              Bit |      20000 | 0.005406 / 0.000000 / 0.000011
0 natives, 2 public callbacks, 1 function calls were not executed.



joaquimandrade 04-20-2009 04:44

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Empowers (Post 809286)
Wow thx for Great TUT!


Profiled:

Code:

date: Sun Apr 19 15:20:28 2009 map: de_dust
type |                            name |      calls | time / min / max
-------------------------------------------------------------------
  n |                  register_clcmd |          2 | 0.000019 / 0.000004 / 0.000015
  p |                      array_test |        10 | 0.064196 / 0.006202 / 0.006642
  p |                      bitsum_test |        10 | 0.029594 / 0.002811 / 0.003570
  p |                      plugin_init |          1 | 0.000002 / 0.000002 / 0.000002
0 natives, 0 public callbacks, 3 function calls were not executed.

Tested on this code:
PHP Code:

#include <amxmodx>

new array[33]
new 
bitsum

public plugin_init() 
{
    array[
32] = false
        
    bitsum 
&=  ~(1<<32
    
    
register_clcmd("array","array_test")
    
register_clcmd("bitsum","bitsum_test")
}

public 
bitsum_test()
{
    for(new 
i;i<=1_000_000;i++)
        if(
bitsum & (1<<32))
            continue;
}

public 
array_test()
{
    for(new 
i;i<=1_000_000;i++)
        if(array[
32])
            continue;


Now I'm thinking should I swap arrays with bitsums in my plugins code. It would use less memory, but it will be harder to understand the code.


Empowers 04-20-2009 04:52

Re: [INFO] Bitsums and Operators
 
Ok forget it man.. I can't talk with u :)

joaquimandrade 04-20-2009 04:53

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Empowers (Post 809887)
Ok forget it man.. I can't talk with u :)

Of course you can. But you need to
Quote:

Read more carefully my post ;)

Exolent[jNr] 04-20-2009 07:10

Re: [INFO] Bitsums and Operators
 
Ahem, andrade.
"continue" is never called because the if() statement never returns true.

Code:
    array[32] = false             bitsum &=  ~(1<<32)

SchlumPF* 04-20-2009 07:43

Re: [INFO] Bitsums and Operators
 
nice idea but in fact this is a very trivial optimization... 1byte vs 132byte... 1byte = ~0.000000001gb, nearly every computer has 1gb ram :/ anyway, this would have been a great idea when computers had ~8mb ram :P
this topic would still make sense if you change its focus from ram optimizations to a general tutorial for bitsums.

Empowers 04-20-2009 08:02

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by SchlumPF* (Post 809968)
nice idea but in fact this is a very trivial optimization... 1byte vs 132byte... 1byte = ~0.000000001gb, nearly every computer has 1gb ram :/ anyway, this would have been a great idea when computers had ~8mb ram :P

But if it is used in EntityThink, addToFullPack hooks. Or somethink that is called very often? I think I would be a good optimization?

SchlumPF* 04-20-2009 08:49

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Empowers (Post 809985)
But if it is used in EntityThink, addToFullPack hooks. Or somethink that is called very often? I think I would be a good optimization?

it would be a very small optimization, yes.

Bugsy 04-20-2009 11:11

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Empowers (Post 809875)
PHP Code:

#define AddFlag(%1,%2)       ( %1 |= ( 1 << (%2-1) ) )
#define RemoveFlag(%1,%2)    ( %1 &= ~( 1 << (%2-1) ) )
#define CheckFlag(%1,%2)     ( %1 & ( 1 << (%2-1) ) ) 

but is this better?
PHP Code:

#define AddFlag(%1,%2)        ( %1 |= ( 1 << (%2 % 32) ) )
#define RemoveFlag(%1,%2)     ( %1 &= ~( 1 << (%2 % 32) ) )
#define CheckFlag(%1,%2)      ( %1 & ( 1 << (%2 % 32) ) ) 


They both do the same thing to prevent a bit-shift of 32 bits. There is no performance change or any other benefit for using either. For a shift of 1-32, -1 will shift 0-31. Using % for a shift of 1-32, it will shift 1-31 normally but when 32 is passed it will shift 0.

The -1 method:
Code:

AddFlag(_,1) = 1 << 0
AddFlag(_,31) = 1 << 30
AddFlag(_,32) = 1 << 31

The modulus method (%):

Code:

AddFlag(_,1) = 1 << 1
AddFlag(_,31) = 1 << 31
AddFlag(_,32) = 1 << 0



All times are GMT -4. The time now is 03:29.

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