473,322 Members | 1,494 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,322 software developers and data experts.

How to replace /(division) operator

spl
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.
Jun 27 '08 #1
16 5224
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
Jun 27 '08 #2
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.
How bad is "the performance" right now? Do you have
evidence that integer divisions are the reason it's bad?

If you do have such evidence, it's probable that the
compiler for a slow-dividing machine already employs lots
of tricks to replace slow divisions with faster alternatives.
If the denominator is a compile-time constant, the compiler
for such a machine will quite likely use all kinds of dodges
to avoid a slow divide. (If both the numerator and denominator
are compile-time constants, it's quite UNlikely that there
will be any divisions at run-time.)

Pretty much the only case where you'll be able to do
better than the compiler is where the division is x/y with
y a run-time variable and where you know things about the
ranges of x and y that the compiler doesn't. In this case
the compiler may generate a fully-general division that
you might be able to replace with something less general but
faster. You'll need to study this on the machine(s) of
interest, because the trade-offs will be different from one
computer to the next.

Finally, as with pretty much anything else: The fastest
division is the one you don't perform at all. If your program
is bogged down in a multitude of divisions, consider ways to
rearrange the calculation so fewer divisions are needed in
the first place.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #3
spl
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!!
Jun 27 '08 #4
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

Jun 27 '08 #5
spl
On Apr 25, 2:03 pm, Chris Dollin <chris.dol...@hp.comwrote:
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.
Jun 27 '08 #6

"spl" <sp**********@gmail.comwrote in message
news:e1**********************************@m44g2000 hsc.googlegroups.com...
>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
Jun 27 '08 #7
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!!
I'll repeat my earlier question for the hard of hearing:
HOW BAD IS "THE PERFORMANCE" RIGHT NOW? DO YOU HAVE
EVIDENCE THAT INTEGER DIVISIONS ARE THE REASON IT'S BAD?
Unless and until you have answers for these questions, you
are simply wasting your time. (Thought experiment: How much
time have you already spent writing your question and reading
the responses, and how many "slow" divisions could your
computer have performed in that amount of time?)

--
Er*********@sun.com
Jun 27 '08 #8
spl wrote:
On Apr 25, 2:03 pm, Chris Dollin <chris.dol...@hp.comwrote:
>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

Jun 27 '08 #9
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.
Jun 27 '08 #10
spl
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.
Jun 27 '08 #11

"spl" <sp**********@gmail.comwrote in message
news:ff**********************************@y38g2000 hsy.googlegroups.com...
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
Jun 27 '08 #12
Bartc wrote:
"spl" <sp**********@gmail.comwrote in message
news:ff**********************************@y38g2000 hsy.googlegroups.com...
>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.
Pay close attention to that "appropriate type" part, and
view "like int" with caution. The problem is that the result
of right-shifting a negative number is implementation-defined,
and is usually not the same as the result of dividing by two.
For example, on the two's complement machines that are all
but universal nowadays the representation of -1 is a string
of 1-bits. Shift the string one place to the right and you
may get another string of 1-bits ("arithmetic shift") or a
single 0-bit followed by 1-bits ("logical shift"). The first
thus gives -1 again, while the second gives a large positive
result -- but neither gives the correct result -1 / 2 == 0.

So: "appropriate type" means either an *unsigned* integer
or a signed integer that you happen to know is non-negative.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #13
spl
Thanks Bartc & Eric. Your suggestions are very useful. By the way all
my variables are unsigned int only. So right shift gives me exact
value.
Jun 27 '08 #14
On 28 Apr., 14:15, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Bartc wrote:
"spl" <splender....@gmail.comwrote in message
news:ff**********************************@y38g2000 hsy.googlegroups.com...
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.

Pay close attention to that "appropriate type" part, and
view "like int" with caution. The problem is that the result
of right-shifting a negative number is implementation-defined,
and is usually not the same as the result of dividing by two.
For example, on the two's complement machines that are all
but universal nowadays the representation of -1 is a string
of 1-bits. Shift the string one place to the right and you
may get another string of 1-bits ("arithmetic shift") or a
single 0-bit followed by 1-bits ("logical shift"). The first
thus gives -1 again, while the second gives a large positive
result -- but neither gives the correct result -1 / 2 == 0.

So: "appropriate type" means either an *unsigned* integer
or a signed integer that you happen to know is non-negative.
Agreed.
There can be even more problems with negative numbers.
IMHO the definition of the division in C89 allows also
-1 / 2 == -1. Although I did not find a C compiler which
does this, it is theoretically possible since in C89 the
division is defined as follows:

The binary / operator yields the quotient, and the % operator
the remainder, of the first operand by the second; if the
second operand is 0, the result is undefined. Otherwise, it
is always true that (a/b)*b + a%b is equal to a. If both
operands are non-negative, the remainder is non-negative and
smaller than the divisor; if not it is guaranteed only that
the absolute value of the remainder is smaller than the
absolute value of the divisor.

As said before this definition allows that the integer
division can be rounded towards minus infinite.
Note that when -1 / 2 == -1 at the same time -1 % 2 == 1

IMHO this definition was chosen to allow integer operations
with one machine instruction.

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
Jun 27 '08 #15
On Apr 28, 10:13*am, spl <splender....@gmail.comwrote:
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.
Write two functions

unsigned int f1 (unsigned int x) { return x / 1024; }
unsigned int f2 (unsigned int x) { return x >10; }

Find a way to make the compiler show the assembler code that it
generates and compare. If you get different code, tell us which
compiler you are using so we can all avoid it. I can't actually
remember any C compiler that wouldn't produce optimal code for the
division, and that is going back more than 20 years.

Jun 27 '08 #16
On May 3, 12:59*pm, "christian.bau" <christian....@cbau.wanadoo.co.uk>
wrote:
On Apr 28, 10:13*am, spl <splender....@gmail.comwrote:
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.

Write two functions

* unsigned int f1 (unsigned int x) { return x / 1024; }
* unsigned int f2 (unsigned int x) { return x >10; }

Find a way to make the compiler show the assembler code that it
generates and compare. If you get different code, tell us which
compiler you are using so we can all avoid it. I can't actually
remember any C compiler that wouldn't produce optimal code for the
division, and that is going back more than 20 years.
Oh, I just noticed you mentioned using a reasonably modern Microsoft
compiler.

In that case, you should also check what code is produced for

unsigned int f3 (unsigned int x) { return x / 1000; }

and ask your lead to explain the code that is generated. Have fun.

Jun 27 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sebastian Haase | last post by:
Hi, I'm interested in having more people in our lab using numarray/NumPy instead of MatLab. For that I have put together a couple useful modules and written many myself. But then I got reminded of...
24
by: Teis Draiby | last post by:
In .NET, can I be sure that the result of a division between two integers always is truncated rather that rounded to nearest? Example: 99 / 50 = 1 regards, Teis
1
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
10
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
9
by: PengYu.UT | last post by:
Hi, The usually integer division will round the result to the biggest integet smaller than the float version division.For example, 10/3 = 3. I'm wondering if there is any easy way to round it...
2
by: kermit | last post by:
For a long time,, There has been a discussion of trueFor division versus integer division in Python. I myslef prefer that / be used for integer division since almost always, I want the...
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
1
by: youjay | last post by:
I've been out of perl for a while, so I am starting from scratch. I have a small applet which scans a set of directories, getting information from some files in each one, and displaying selected...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.