473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
5 3309
Banfa
9,065 Expert Mod 8TB
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
use gettimeoftheday() before and after the mask option.Same with toupper
Jun 1 '10 #3
prn
254 Expert 100+
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
9,065 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
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

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

Similar topics

2
by: Joe Gonzalez | last post by:
Is it possible to perform bitwise operations in selects in DB2 8.1? In postgres I can say something like SELECT ... FROM <sometable> WHERE RecFlags && 0x08 <> 0 to get a bitwise and operation. I...
13
by: Manish_Ganvir | last post by:
Please do not use pointer arithmetic or for loops Solution
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
13
by: JanWhitney | last post by:
I am learning C++, so please be kind. Is there anywhere that I can view the source code for the toUpper function? Thanks.
10
by: Rob Wilkerson | last post by:
I'm attempting to do some work around existing code that uses bitwise operations to manage flags passed into a function and I'm quite frankly unequipped to do so. I've never done much with bitwise...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
3
by: ravichobey | last post by:
Hi all, How to implement sizeof() function using C programming? Regards, Ravi
14
by: Aftabpasha | last post by:
Is it possible to perform all arithmetic operations (+,-,*,/) using Bitwise operations only. Please consider signed numbers also. Thank You.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.