Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to replace /(division) operator

Reply
Thread Tools

How to replace /(division) operator

 
 
spl
Guest
Posts: n/a
 
      04-25-2008
To increase the performance, how to change the / operator with bitwise
operators?
for ex: 25/5, 225/25 or 25/3 or any division, but I am not bothered
of any remainder.
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      04-25-2008
spl wrote:
> To increase the performance, how to change the / operator with bitwise
> operators?
> for ex: 25/5, 225/25 or 25/3 or any division, but I am not bothered
> of any remainder.


This is not meaningful if you do not say which CPU
are you using. Division is not that expensive anymore,
and the extra code for implementing division with bitwise
operators could very well be MUCH slower.

The lcc-win compiler will replace certain kinds of division ( divisions
by an integer constant) with 3-4 instructions with shifts and adds. This
is only possible when the divisor is known at compile time.



--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
 
 
 
spl
Guest
Posts: n/a
 
      04-25-2008
I use normal Microsoft visual C++ compiler only. Due to lots of more
frequency in accessing / operator, I feel to change it with bitwise
operator, as it is always faster then / operator. So, If you know
bitwise manipulation, please suggest me!!
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      04-25-2008
spl wrote:

> I use normal Microsoft visual C++ compiler only. Due to lots of more
> frequency in accessing / operator, I feel to change it with bitwise
> operator, as it is always faster then / operator.


Does your application have an actual, measured, performance problem
that you've tracked down to your use of `/`?

Sure, individual bitwise operations are often faster than individual
`/` operations. That's because they do simpler -- different -- things.
To replace your `/`s with bitwise ops, you'll have to do /multiple/,
/dependent/ bitwise ops, so it's not longer obvious that this is
faster.

--
"Your world, Colonel, and I wish you the best of it!" /Witch World/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
spl
Guest
Posts: n/a
 
      04-25-2008
On Apr 25, 2:03 pm, Chris Dollin <chris.dol...@hp.com> wrote:
> spl wrote:
> > I use normal Microsoft visual C++ compiler only. Due to lots of more
> > frequency in accessing / operator, I feel to change it with bitwise
> > operator, as it is always faster then / operator.

>
> Does your application have an actual, measured, performance problem
> that you've tracked down to your use of `/`?


YES. I have.
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      04-25-2008

"spl" <> wrote in message
news:e1ce6bf0-d4b5-4fa0-a647-...
>I use normal Microsoft visual C++ compiler only. Due to lots of more
> frequency in accessing / operator, I feel to change it with bitwise
> operator, as it is always faster then / operator. So, If you know
> bitwise manipulation, please suggest me!!


Do you know what sort of numbers are being operated on?

Are the numbers you divide by, constants? If not things are more difficult.

Dividing by a power of 2 can sometimes be replaced by a shift, if you know
the number. (If not, but is a power of 2, you can keep track of the shift
count needed.) However, if it's something obvious, the compiler will already
have done it!

How much runtime is given over to division anyway? On what processor (as
they are all different)?

Can you post some test code that demonstrates the problem you have?

Sometimes you can rearrange the code to reduce or eliminate division.

--
Bart


 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      04-25-2008
spl wrote:

> On Apr 25, 2:03 pm, Chris Dollin <chris.dol...@hp.com> wrote:
>> spl wrote:
>> > I use normal Microsoft visual C++ compiler only. Due to lots of more
>> > frequency in accessing / operator, I feel to change it with bitwise
>> > operator, as it is always faster then / operator.

>>
>> Does your application have an actual, measured, performance problem
>> that you've tracked down to your use of `/`?

>
> YES. I have.


Then you can tell us the numbers and the context and the code
that is slow, and get advice suited to your actual problem
rather than speculation into the vacuum.

(And note that you'll get /C/ advice; if you have a C++ program,
it's possible that there will be better C++-oriented answers
in A Different Group.)

--
"It was the first really clever thing the King had /Alice in Wonderland/
said that day."

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-25-2008
spl wrote:
> I use normal Microsoft visual C++ compiler only. Due to lots of more
> frequency in accessing / operator, I feel to change it with bitwise
> operator, as it is always faster then / operator. So, If you know
> bitwise manipulation, please suggest me!!


You are probably confused about what language you are using. The C
programing language (the one discussed in the newsgroup you posted to,
<news:comp.lang.c>) does not allow you to redefine the meaning of
operators like '/'.

There are other puzzling things about your post. If you actually knew
the "bitwise operator, as it is always faster than / operator", then you
would already have the implementation you want. In that case, you would
have no need of anyone to "please suggest me!!". And if your claim were
in any sense true, why would you think that the Microsoft compiler
developers did not already use this supposed "always faster" approach
for their implementation of the '/' operator?

I fear that you are very confused, or erroneously parroting someone
else, or simply a troll.
 
Reply With Quote
 
spl
Guest
Posts: n/a
 
      04-28-2008
Sorry for the delay in getting back to your questions, Actually
changing the division operator to bitwise operators is suggested by my
tech lead. As she done so many such improvement by doing this and she
is having enough evidence for the same. She suggested me to do the
same in my current project too. Actually I want to divide some big
number with constant number, say 1024 always. Please give me your
suggestion please.
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      04-28-2008

"spl" <> wrote in message
news:ff9f8829-852d-4ab4-b05f-...
> Sorry for the delay in getting back to your questions, Actually
> changing the division operator to bitwise operators is suggested by my
> tech lead. As she done so many such improvement by doing this and she
> is having enough evidence for the same. She suggested me to do the
> same in my current project too. Actually I want to divide some big
> number with constant number, say 1024 always. Please give me your
> suggestion please.


My suggestion is to just divide by 1024.

Your compiler will use the most appropriate coding, you probably don't even
have to tell it to optimise.

Only if your compiler is so basic that you might try using (A>>10) instead
of A/1024, if A is an appropriate type like int, followed by a comment as to
why you are doing this.

--
Bartc


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
Re: [Pyrex] pyrex functions to replace a method (Re: replace a method Greg Ewing Python 2 06-29-2006 05:25 PM
pyrex functions to replace a method (Re: replace a method in class:how?) Brian Blais Python 1 06-27-2006 12:13 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57