By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,375 Members | 542 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,375 IT Pros & Developers. It's quick & easy.

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

P: 32
Hello,
I was trying to implement toupper() functionality using bitwise operations (assuming only alphabets as input).
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int main()
  5. {
  6.     int i=0;
  7.     unsigned int mask = ~32;
  8.     char  buffer[512] = "Hello there THis IS better Than THE Best";
  9.  
  10.     printf("%s\n",buffer);
  11.  
  12.     while(buffer[i]!='\0')
  13.         printf("%c",mask & buffer[i++]);    ////  Slow
  14.      // printf("%c",toupper(buffer[i++]));   ////   Fast....why?
  15.  
  16.     return 0;
  17. }
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.
May 30 '10 #1
Share this Question
Share on Google+
5 Replies

Banfa
Expert Mod 5K+
P: 8,972
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.
May 30 '10 #2

P: 13
use gettimeoftheday() before and after the mask option.Same with toupper
Jun 1 '10 #3

prn
Expert 100+
P: 254
prn
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
Jun 1 '10 #4

Banfa
Expert Mod 5K+
P: 8,972
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.
Jun 1 '10 #5

Expert 100+
P: 2,418
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.
Jun 1 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.