toupper() function implementation using Bitwise operations --- Speed difference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aftabpasha
    New Member
    • Oct 2008
    • 32

    toupper() function implementation using Bitwise operations --- Speed difference

    Hello,
    I was trying to implement toupper() functionality using bitwise operations (assuming only alphabets as input).
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
        int i=0;
        unsigned int mask = ~32;
        char  buffer[512] = "Hello there THis IS better Than THE Best";
        
        printf("%s\n",buffer);
        
        while(buffer[i]!='\0')
            printf("%c",mask & buffer[i++]);    ////  Slow
         // printf("%c",toupper(buffer[i++]));   ////   Fast....why?
        
        return 0;
    }
    It works. The thing that I am not getting is that it's execution time is 0.031s.
    But when I used library function toupper() to implement the same functionality, its execution time is almost 0.000s. I thought my bitwise implementation would be faster, as it is avoiding many other overheads like function call, no if-else check for alphabets (as i've assumed input would always be alphabets) etc. Can someone explain why so?
    I'm using CodeBlocks 8.02 IDE with GNU GCC Compiler.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    To start will you are not testing the speed of the toupper functionality but the speed of toupper conversion plus printing.

    Since printing is a relatively slow operation what you are mainly measuring is the speed of printing and since that is a system service you have no guarantee that it wont be held up by some other process.

    So I would say your results are meaning less. Re-write your test so that the only thing you are measuring the speed of is the conversion.

    Also the standard library writers are not fools you can pretty much assume that it will have been implemented in the most efficient method possible.

    Comment

    • mayankarpit
      New Member
      • Jun 2010
      • 13

      #3
      use gettimeoftheday () before and after the mask option.Same with toupper

      Comment

      • prn
        Recognized Expert Contributor
        • Apr 2007
        • 254

        #4
        Also, if your version of toupper() is to have any generality, keep in mind that although the difference between À (uc A with grave, in case the actual character does not show up for you) and à (lc a with grave) is 32, just like the unaccented characters, the difference between Č (uc C with "caron") and č (lc c with "caron") is only 1. IOW, just flipping the 32-bit may fail if you are concerned with anything more than plain English. (You may not be.)

        Paul

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          And of course flipping bit 32 may also fail if the execution character set for your compiler is not ASCII.

          i.e. the solution is inherently non-portable.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            I recommend you put braces around the body of the line-12 while loop. You probably intend to always have one line commented out, but if you forget and leave both in then the loop will fool you.

            Comment

            Working...