Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: In-place bitwise NOT?

by Perlbotics (Abbot)
on Jul 26, 2013 at 18:02 UTC ( #1046576=note: print w/ replies, xml ) Need Help??


in reply to In-place bitwise NOT?

I am not sure, if this works as you expect... maybe the macros and wrappers generate a copy under the hood?

use Inline C; use strict; use warnings; my $x = "012\xf0ABC\x0f\x00\x01\xff"; print unpack("H*", $x ), "\n"; # 303132f04142430f0001ff flipbits( $x, length $x ); print unpack("H*", $x ), "\n"; # cfcecd0fbebdbcf0fffe00 __END__ __C__ void flipbits(char* a, int len ) { for ( ; len-- ; ++a ) *a ^= 0xff; }

Update: Switching to 32bit blocks gave a huge improvement on my 32bit setup...

void flipbits32(char* a, int len ) { int blocks = len >> 2; if ( sizeof blocks == 4 ) { /* try 32bit blocks */ for ( ; blocks-- ; a += 4 ) *( (unsigned int*) a ) ^= 0xffffffffU +; /* TODO: add treatment for last 1..3 bytes if len is not a multipl +e of 4 */ } else { /* fallback to 8bit */ for ( ; len-- ; ++a ) *a ^= 0xff; } }
Benchmark:
Rate str inline inline32 str 69.1/s -- -37% -69% flipbits 110/s 59% -- -51% flipbits32 223/s 222% 103% --

Potential next steps which are also less general, but that's the price to pay:


Comment on Re: In-place bitwise NOT?
Select or Download Code
Re^2: In-place bitwise NOT?
by BrowserUk (Pope) on Jul 26, 2013 at 19:54 UTC

    That certainly works and doesn't appear to do any copying. It comes out at nearly 50% faster than tr//:

    C:\test>1046579 -N=2**20 Rate str translate C str 656/s -- -8% -37% translate 713/s 9% -- -32% C 1047/s 60% 47% --

    The only downside is that it imposes the need for a compiler on a module that otherwise doesn't need one.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046576]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2014-07-26 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (179 votes), past polls