Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Inline CPP undefined subroutine

by kikuchiyo (Hermit)
on Nov 05, 2018 at 21:11 UTC ( [id://1225290]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Inline CPP undefined subroutine
in thread Inline CPP undefined subroutine

Now that the question is considered solved, this piece of code might serve as an interesting side note about (premature) optimization.

I was going to observe that ord('C') = 0x43 and ord('G') = 0x47, so you could do the comparison in one step (if (*gcString | 4 == 'G') or if (*gcString & 0xFB == 'C')), and perhaps compare 8 bytes in one go by casting the char * pointer to uint64 * and doing the necessary accounting.

Then it occurred to me to check the code the compiler actually generates from the simple and readable function above. There is a nice online service at godbolt.org that lets you do exactly that. Paste the function text into the source window (and add the necessary #include <cstring> header to make it compile), enter -O3 for compiler options, and behold. GCC 8.2 not only notices the similar ASCII codes and uses a trick similar to mine, but it generates an efficient but nearly unreadable main loop using SIMD instructions that compares 16 bytes in one go (which is better than what you can do with simple, standard C).

I also had the idea of replacing the loop in the function with while (*gcString++), thinking that strlen needlessly scans through the string once to find the terminator, but guess what - this kills the optimization. It needs to know the length in advance to be able to do the advanced SIMD loop.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found