(Warning:very limited expertise.)

Nothing obviously wrong leaps out from what you've posted.

But that isn't going to be any quicker to use than the equivalent Perl code:

vec( $vector, $bit, 1 ) ||= 1;

A quick test shows that it is considerably slower:

use strict; use warnings; use Benchmark qw[ cmpthese ]; use Inline C => 'DATA'; my( $vec1, $vec2 ) = ( ( chr(0) x 125000 ) x 2 ); cmpthese -3, { inline => sub { mytest( $vec1, $_ ) for 0 .. 1e6-1; }, vec => sub { vec( $vec2, $_, 1 ) ||= 1 for 0 .. 1e6-1; }, }; warn "Different results" unless $vec1 eq $vec2; __DATA__ __C__ int mytest(SV* sv_vec, unsigned int bit) { STRLEN vecbytes; // Length of vector in bytes unsigned char *myvec = (unsigned char *) SvPV(sv_vec, vecbytes); if (bit/8 >= vecbytes) return 0; // Check in range if (myvec[bit/8] & 1U<<(bit%8)) return 1; // Test if a bit is set myvec[bit/8] |= 1U<<(bit%8); // Set bit (CHANGES $vector) return 1; }

Results:

C:\test>903727.pl Rate inline vec inline 3.11/s -- -60% vec 7.77/s 150% -- C:\test>903727.pl Rate inline vec inline 3.10/s -- -60% vec 7.77/s 151% --

You mention in a later post that "The real case applies some fairly complex logic to multiple large bit vectors (each 1m+ bits) which runs a lot faster in C.". That raises a couple of questions:

Update: There is also the question of why you are setting the bit conditionally? That is, your calling code will never be able to tell the difference between the situation where the bit was previously unset; and when it was already set.

So why bother testing if it is set and not just set it?


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.

In reply to Re: Bit vector fiddling with Inline C by BrowserUk
in thread Bit vector fiddling with Inline C by oxone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.