Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. The quest for fastest strstr-like function in C

The quest for fastest strstr-like function in C

Scheduled Pinned Locked Moved Clever Code
performancecssalgorithmsregexhelp
12 Posts 8 Posters 64 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sanmayce

    Hi to all C programmers, my desire is with help of more experienced guys/girls to boost the following function:

    // ### Mix(2in1) of Karp-Rabin & Boyer-Moore-Horspool algorithm [
    // Caution: For better speed the case 'if (cbPattern==1)' was removed, so Pattern must be longer than 1 char.
    char * Railgun_Quadruplet (char * pbTarget,
    char * pbPattern,
    unsigned long cbTarget,
    unsigned long cbPattern)
    {
    char * pbTargetMax = pbTarget + cbTarget;
    register unsigned long ulHashPattern;
    unsigned long ulHashTarget;
    unsigned long count;
    unsigned long countSTATIC;
    // unsigned long countRemainder;

    /*
    const unsigned char SINGLET = *(char *)(pbPattern);
    const unsigned long Quadruplet2nd = SINGLET<<8;
    const unsigned long Quadruplet3rd = SINGLET<<16;
    const unsigned long Quadruplet4th = SINGLET<<24;
    */
    unsigned char SINGLET;
    unsigned long Quadruplet2nd;
    unsigned long Quadruplet3rd;
    unsigned long Quadruplet4th;

    unsigned long  AdvanceHopperGrass;
    
    long i; //BMH needed
    int a, j, bm\_bc\[ASIZE\]; //BMH needed
    unsigned char ch; //BMH needed
    

    // unsigned char lastch, firstch; //BMH needed

    if (cbPattern > cbTarget)
        return(NULL);
    

    // Doesn't work when cbPattern = 1
    // The next IF-fragment works very well with cbPattern>1, OBVIOUSLY IT MUST BE UNROLLED(but crippled with less functionality) SINCE either cbPattern=2 or cbPattern=3!
    if ( cbPattern<4) { // This IF makes me unhappy: it slows down from 390KB/clock to 367KB/clock for 'fast' pattern. This fragment(for 2..3 pattern lengths) is needed because I need a function different than strchr but sticking to strstr i.e. lengths above 1 are to be handled.
    pbTarget = pbTarget+cbPattern;
    ulHashPattern = ( (*(char *)(pbPattern))<<8 ) + *(pbPattern+(cbPattern-1));
    // countSTATIC = cbPattern-2;

    if ( cbPattern==3) {
    for ( ;; )
    {
    if ( ulHashPattern == ( (*(char *)(pbTarget-3))<<8 ) + *(pbTarget-1) ) {
    if ( *(char *)(pbPattern+1) == *(char *)(pbTarget-2) ) return((pbTarget-3));
    }
    if ( (char)(ulHashPattern>>8) != *(pbTarget-2) ) pbTarget++;
    pbTarget++;
    if (pbTarget > pbTargetMax)
    return(NULL);
    }
    } else {
    }
    for ( ;; )
    {
    // The line below gives for 'cbPattern'>=1:
    // Karp_Rabin_Kaze_4_OCTETS_hits/Karp_Rabin_Kaze_4_OCTETS_clocks: 4/543
    // Karp_Rabin_Kaze_4_OCTETS performance: 372KB/clock
    /*
    if ( (ulHashPattern

    R Offline
    R Offline
    Reiss
    wrote on last edited by
    #2

    Hi, This is a programming question and as such it is posted in the wrong place (It states no programming questions should be posted here in bold red letters) - try posting in the c forum instead

    1 Reply Last reply
    0
    • S Sanmayce

      Hi to all C programmers, my desire is with help of more experienced guys/girls to boost the following function:

      // ### Mix(2in1) of Karp-Rabin & Boyer-Moore-Horspool algorithm [
      // Caution: For better speed the case 'if (cbPattern==1)' was removed, so Pattern must be longer than 1 char.
      char * Railgun_Quadruplet (char * pbTarget,
      char * pbPattern,
      unsigned long cbTarget,
      unsigned long cbPattern)
      {
      char * pbTargetMax = pbTarget + cbTarget;
      register unsigned long ulHashPattern;
      unsigned long ulHashTarget;
      unsigned long count;
      unsigned long countSTATIC;
      // unsigned long countRemainder;

      /*
      const unsigned char SINGLET = *(char *)(pbPattern);
      const unsigned long Quadruplet2nd = SINGLET<<8;
      const unsigned long Quadruplet3rd = SINGLET<<16;
      const unsigned long Quadruplet4th = SINGLET<<24;
      */
      unsigned char SINGLET;
      unsigned long Quadruplet2nd;
      unsigned long Quadruplet3rd;
      unsigned long Quadruplet4th;

      unsigned long  AdvanceHopperGrass;
      
      long i; //BMH needed
      int a, j, bm\_bc\[ASIZE\]; //BMH needed
      unsigned char ch; //BMH needed
      

      // unsigned char lastch, firstch; //BMH needed

      if (cbPattern > cbTarget)
          return(NULL);
      

      // Doesn't work when cbPattern = 1
      // The next IF-fragment works very well with cbPattern>1, OBVIOUSLY IT MUST BE UNROLLED(but crippled with less functionality) SINCE either cbPattern=2 or cbPattern=3!
      if ( cbPattern<4) { // This IF makes me unhappy: it slows down from 390KB/clock to 367KB/clock for 'fast' pattern. This fragment(for 2..3 pattern lengths) is needed because I need a function different than strchr but sticking to strstr i.e. lengths above 1 are to be handled.
      pbTarget = pbTarget+cbPattern;
      ulHashPattern = ( (*(char *)(pbPattern))<<8 ) + *(pbPattern+(cbPattern-1));
      // countSTATIC = cbPattern-2;

      if ( cbPattern==3) {
      for ( ;; )
      {
      if ( ulHashPattern == ( (*(char *)(pbTarget-3))<<8 ) + *(pbTarget-1) ) {
      if ( *(char *)(pbPattern+1) == *(char *)(pbTarget-2) ) return((pbTarget-3));
      }
      if ( (char)(ulHashPattern>>8) != *(pbTarget-2) ) pbTarget++;
      pbTarget++;
      if (pbTarget > pbTargetMax)
      return(NULL);
      }
      } else {
      }
      for ( ;; )
      {
      // The line below gives for 'cbPattern'>=1:
      // Karp_Rabin_Kaze_4_OCTETS_hits/Karp_Rabin_Kaze_4_OCTETS_clocks: 4/543
      // Karp_Rabin_Kaze_4_OCTETS performance: 372KB/clock
      /*
      if ( (ulHashPattern

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #3

      If you cool down the processor using Liquid Nitrogen, you should be able to achieve BACON time matches. Add a CListCtrl based front end and Robert's your Mother's Brother.


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      J D 2 Replies Last reply
      0
      • N Nagy Vilmos

        If you cool down the processor using Liquid Nitrogen, you should be able to achieve BACON time matches. Add a CListCtrl based front end and Robert's your Mother's Brother.


        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

        J Offline
        J Offline
        Julien Villers
        wrote on last edited by
        #4

        BINGO!

        'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood

        S 1 Reply Last reply
        0
        • N Nagy Vilmos

          If you cool down the processor using Liquid Nitrogen, you should be able to achieve BACON time matches. Add a CListCtrl based front end and Robert's your Mother's Brother.


          Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #5

          Couldn't have put it better myself :)

          Dave Find Me On: Web|Facebook|Twitter|LinkedIn


          Folding Stats: Team CodeProject

          1 Reply Last reply
          0
          • S Sanmayce

            Hi to all C programmers, my desire is with help of more experienced guys/girls to boost the following function:

            // ### Mix(2in1) of Karp-Rabin & Boyer-Moore-Horspool algorithm [
            // Caution: For better speed the case 'if (cbPattern==1)' was removed, so Pattern must be longer than 1 char.
            char * Railgun_Quadruplet (char * pbTarget,
            char * pbPattern,
            unsigned long cbTarget,
            unsigned long cbPattern)
            {
            char * pbTargetMax = pbTarget + cbTarget;
            register unsigned long ulHashPattern;
            unsigned long ulHashTarget;
            unsigned long count;
            unsigned long countSTATIC;
            // unsigned long countRemainder;

            /*
            const unsigned char SINGLET = *(char *)(pbPattern);
            const unsigned long Quadruplet2nd = SINGLET<<8;
            const unsigned long Quadruplet3rd = SINGLET<<16;
            const unsigned long Quadruplet4th = SINGLET<<24;
            */
            unsigned char SINGLET;
            unsigned long Quadruplet2nd;
            unsigned long Quadruplet3rd;
            unsigned long Quadruplet4th;

            unsigned long  AdvanceHopperGrass;
            
            long i; //BMH needed
            int a, j, bm\_bc\[ASIZE\]; //BMH needed
            unsigned char ch; //BMH needed
            

            // unsigned char lastch, firstch; //BMH needed

            if (cbPattern > cbTarget)
                return(NULL);
            

            // Doesn't work when cbPattern = 1
            // The next IF-fragment works very well with cbPattern>1, OBVIOUSLY IT MUST BE UNROLLED(but crippled with less functionality) SINCE either cbPattern=2 or cbPattern=3!
            if ( cbPattern<4) { // This IF makes me unhappy: it slows down from 390KB/clock to 367KB/clock for 'fast' pattern. This fragment(for 2..3 pattern lengths) is needed because I need a function different than strchr but sticking to strstr i.e. lengths above 1 are to be handled.
            pbTarget = pbTarget+cbPattern;
            ulHashPattern = ( (*(char *)(pbPattern))<<8 ) + *(pbPattern+(cbPattern-1));
            // countSTATIC = cbPattern-2;

            if ( cbPattern==3) {
            for ( ;; )
            {
            if ( ulHashPattern == ( (*(char *)(pbTarget-3))<<8 ) + *(pbTarget-1) ) {
            if ( *(char *)(pbPattern+1) == *(char *)(pbTarget-2) ) return((pbTarget-3));
            }
            if ( (char)(ulHashPattern>>8) != *(pbTarget-2) ) pbTarget++;
            pbTarget++;
            if (pbTarget > pbTargetMax)
            return(NULL);
            }
            } else {
            }
            for ( ;; )
            {
            // The line below gives for 'cbPattern'>=1:
            // Karp_Rabin_Kaze_4_OCTETS_hits/Karp_Rabin_Kaze_4_OCTETS_clocks: 4/543
            // Karp_Rabin_Kaze_4_OCTETS performance: 372KB/clock
            /*
            if ( (ulHashPattern

            S Offline
            S Offline
            Sanmayce
            wrote on last edited by
            #6

            I did read what was stated, but along with question the code being the fastest (to my knowledge) fits well in 'clever code' context, obviously you don't think so, so feel free to delete it. And I didn't expect such negative reactions from old members, maybe you are too versed in C to disregard such code without giving even the tiny argument.

            Get down get down get down get it on show love and give it up What are you waiting on?

            F 1 Reply Last reply
            0
            • S Sanmayce

              I did read what was stated, but along with question the code being the fastest (to my knowledge) fits well in 'clever code' context, obviously you don't think so, so feel free to delete it. And I didn't expect such negative reactions from old members, maybe you are too versed in C to disregard such code without giving even the tiny argument.

              Get down get down get down get it on show love and give it up What are you waiting on?

              F Offline
              F Offline
              Firo Atrum Ventus
              wrote on last edited by
              #7

              Sanmayce wrote:

              obviously you don't think so

              Now, now, we all act like this because your post started with a "here's my best code, can someone improve it?" that classified as programming question. I suggest you changed it to something like "Here's a code to optimize xxx how's your opinion about it?". I'm sure you'll get much better response that way.

              Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

              1 Reply Last reply
              0
              • J Julien Villers

                BINGO!

                'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood

                S Offline
                S Offline
                saman biook aghazadeh
                wrote on last edited by
                #8

                are you kidding?

                N 1 Reply Last reply
                0
                • S saman biook aghazadeh

                  are you kidding?

                  N Offline
                  N Offline
                  Nagy Vilmos
                  wrote on last edited by
                  #9

                  No. I was kidding, he was calling bingo as I hit almost every CP meme in a single post. I missed out the delightful Miss Hayek[^].


                  Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                  R 1 Reply Last reply
                  0
                  • N Nagy Vilmos

                    No. I was kidding, he was calling bingo as I hit almost every CP meme in a single post. I missed out the delightful Miss Hayek[^].


                    Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                    R Offline
                    R Offline
                    Roger Allen
                    wrote on last edited by
                    #10

                    You forgot a latex suit mention. :-O

                    If you vote me down, my score will only get lower

                    N 1 Reply Last reply
                    0
                    • R Roger Allen

                      You forgot a latex suit mention. :-O

                      If you vote me down, my score will only get lower

                      N Offline
                      N Offline
                      Nagy Vilmos
                      wrote on last edited by
                      #11

                      It is a "Latex Appendage Suit" and I'm not going near it after John...


                      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                      R 1 Reply Last reply
                      0
                      • N Nagy Vilmos

                        It is a "Latex Appendage Suit" and I'm not going near it after John...


                        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                        R Offline
                        R Offline
                        Roger Allen
                        wrote on last edited by
                        #12

                        In the words of Hong Kong Phooey..... Could be!

                        If you vote me down, my score will only get lower

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups