I was trying to implement toupper() functionality using bitwise operations (assuming only alphabets as input).
Expand|Select|Wrap|Line Numbers
- #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;
- }
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.