473,405 Members | 2,210 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,405 software developers and data experts.

performance

I am looking for differences of cost of an equality test between 2
integers and a multiplication between 2 integers.

Which operation is most costly : multiplication (*) or equality test (==)
Jul 22 '05 #1
8 1707

"Gregory Noulin" <gr************@inrialpes.fr> wrote in message
news:c0**********@trompette.imag.fr...
I am looking for differences of cost of an equality test between 2
integers and a multiplication between 2 integers.

Which operation is most costly : multiplication (*) or equality test (==)


It depends entirely on your platform. C++ does not specify anything about
the cost of these operations.

Since the two operations do completely different things I can't understand
why you'd want to know which was faster. Please explain.

john
Jul 22 '05 #2
"Gregory Noulin" <gr************@inrialpes.fr> wrote in message
news:c0**********@trompette.imag.fr...
I am looking for differences of cost of an equality test between 2
integers and a multiplication between 2 integers.

Which operation is most costly : multiplication (*) or equality test (==)


That depends on the CPU Arch. and the code produced by the compiler to do
the comparison or multiplication.

For example, on x86, the comparison might be like this:

if (x == y)

mov ax, x
xor ax, y
jz _yes_equal

And multiplication:

x * y

mov bx, x
mov ax, y
mul bx

So the answer relies on whether "xor" is executed faster than "mul"
As I remember, yes , "xor" is faster.

In most cases the comparison is faster than multiplication.

--
Elias
Jul 22 '05 #3

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c0*************@ID-196037.news.uni-berlin.de...

"Gregory Noulin" <gr************@inrialpes.fr> wrote in message
news:c0**********@trompette.imag.fr...
I am looking for differences of cost of an equality test between 2
integers and a multiplication between 2 integers.

Which operation is most costly : multiplication (*) or equality test
(==)
It depends entirely on your platform.


On some platforms it also depends on how predictable the outcome of the
equality test will be. I.e. there no way to answer this question without
going into the specifics.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #4
John Harrison wrote:
"Gregory Noulin" <gr************@inrialpes.fr> wrote in message
news:c0**********@trompette.imag.fr...
I am looking for differences of cost of an equality test between 2
integers and a multiplication between 2 integers.

Which operation is most costly : multiplication (*) or equality test (==)

It depends entirely on your platform. C++ does not specify anything about
the cost of these operations.

Since the two operations do completely different things I can't understand
why you'd want to know which was faster. Please explain.

john


I have a data set and i have the choice between 2 structures. I have a
computation formula which needs to access to element of data.
On the first hand, i have a direct access to element (but a big big
structure) and in the formula i make useless operations (addition,
multiplication).
On the other hand, i must make a test (==) to access to the good element
(but a smaller structure) and in the formula i don't make any useless
operations.

I just want to know if a test isn't too costly in order to choose the
smaller structure
Jul 22 '05 #5

"Gregory Noulin" <gr************@inrialpes.fr> wrote in message
news:c0**********@trompette.imag.fr...
John Harrison wrote:
"Gregory Noulin" <gr************@inrialpes.fr> wrote in message
news:c0**********@trompette.imag.fr...
I am looking for differences of cost of an equality test between 2
integers and a multiplication between 2 integers.

Which operation is most costly : multiplication (*) or equality test
(==)

It depends entirely on your platform. C++ does not specify anything about the cost of these operations.

Since the two operations do completely different things I can't understand why you'd want to know which was faster. Please explain.

john


I have a data set and i have the choice between 2 structures. I have a
computation formula which needs to access to element of data.
On the first hand, i have a direct access to element (but a big big
structure) and in the formula i make useless operations (addition,
multiplication).
On the other hand, i must make a test (==) to access to the good element
(but a smaller structure) and in the formula i don't make any useless
operations.

I just want to know if a test isn't too costly in order to choose the
smaller structure


I would be surprised if it made any noticeable difference at all. Your
choice should usually be governed by which is easier to understand, more
flexible to change, etc. etc. Efficiency would normally not be a
consideration at all.

So unless you have specific efficiency considerations to meet I'd forget
about it. It's a very common newbie trait to be needlessly concerned about
'efficiency' while ignoring the genuine efficiency issues like writing easy
to understand code. (Apologies if you are not a newbie).

If you really think that this is important, then the only way is to try both
choices and time them. But of course the answers you get will not
necessarily be applicable to any other machine or environment.

john
Jul 22 '05 #6
"lallous" <la*****@lgwm.org> wrote in message news:<c0*************@ID-161723.news.uni-berlin.de>...
"Gregory Noulin" <gr************@inrialpes.fr> wrote in message
news:c0**********@trompette.imag.fr...
I am looking for differences of cost of an equality test between 2
integers and a multiplication between 2 integers.

Which operation is most costly : multiplication (*) or equality test (==)


That depends on the CPU Arch. and the code produced by the compiler to do
the comparison or multiplication.

For example, on x86, the comparison might be like this:

if (x == y)

mov ax, x
xor ax, y
jz _yes_equal

And multiplication:

x * y

mov bx, x
mov ax, y
mul bx

So the answer relies on whether "xor" is executed faster than "mul"
As I remember, yes , "xor" is faster.

In most cases the comparison is faster than multiplication.

As far as I know, XOR takes only one clock on x86 compatibles (386 and
higher).
As to MUL, it takes much more clocks to complete (at least 2).

There is a good book on ASM at http://webster.cs.ucr.edu/AoA.html
Another source is at http://www.masm32.com/
MASM32 package contains a lot of help files where the timings of
various asm commands are specified, but, if I am not wrong, the
timings are specified only up to x386.

As for the latest P4 CPU(s), the best source is
http://www.intel.com/design/Pentium/manuals/

I hope this info will help :)
Jul 22 '05 #7
Peter van Merkerk wrote:

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c0*************@ID-196037.news.uni-berlin.de...

"Gregory Noulin" <gr************@inrialpes.fr> wrote in message
news:c0**********@trompette.imag.fr...
> I am looking for differences of cost of an equality test between 2
> integers and a multiplication between 2 integers.
>
> Which operation is most costly : multiplication (*) or equality
> test

(==)

It depends entirely on your platform.


On some platforms it also depends on how predictable the outcome of
the equality test will be. I.e. there no way to answer this question
without going into the specifics.


Further, it depends on the size of the integers. If the integer type is
bigger than the maximum integer sizes supported by the CPU, it will be
emulated.

Jul 22 '05 #8
On Tue, 10 Feb 2004 16:08:41 +0100, Gregory Noulin
<gr************@inrialpes.fr> wrote in comp.lang.c++:
John Harrison wrote:
"Gregory Noulin" <gr************@inrialpes.fr> wrote in message
news:c0**********@trompette.imag.fr...
I am looking for differences of cost of an equality test between 2
integers and a multiplication between 2 integers.

Which operation is most costly : multiplication (*) or equality test (==)

It depends entirely on your platform. C++ does not specify anything about
the cost of these operations.

Since the two operations do completely different things I can't understand
why you'd want to know which was faster. Please explain.

john


I have a data set and i have the choice between 2 structures. I have a
computation formula which needs to access to element of data.
On the first hand, i have a direct access to element (but a big big
structure) and in the formula i make useless operations (addition,
multiplication).
On the other hand, i must make a test (==) to access to the good element
(but a smaller structure) and in the formula i don't make any useless
operations.

I just want to know if a test isn't too costly in order to choose the
smaller structure


No general answer is possible. The C++ language defines how various
things work. It does not specify relative speed or efficiency of
anything. There are processors, unlike the Pentium and others used in
Windows PCs, that can do integer multiplications in a single clock
cycle, just as fast as they can do an integer comparison. In fact
there are some that can do floating point multiplications in a single
clock cycle.

If you absolutely need to know which is faster on your platform
running code generated by your compiler, write some test programs
using both methods and do some timing tests.

Generally the best approach is to write your program using the method
that generates the clearest, most easily read and understood code.
Then, when your program works correctly and only then, if the
performance of the program is not good enough, use a tool like a
profile to find the bottle necks. If and only if this routine is a
significant bottle neck in a too-slow program, experiment with ways to
speed it up.

If you worry about things like this ahead of time, you often wind up
spending a lot of effort on something that only makes a 1/10th of 1%
difference in the speed of the final code.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #9

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

Similar topics

25
by: Brian Patterson | last post by:
I have noticed in the book of words that hasattr works by calling getattr and raising an exception if no such attribute exists. If I need the value in any case, am I better off using getattr...
12
by: Fred | last post by:
Has anyone a link or any information comparing c and c++ as far as execution speed is concerned? Signal Processing algorithms would be welcome... Thanks Fred
12
by: serge | last post by:
I have an SP that is big, huge, 700-800 lines. I am not an expert but I need to figure out every possible way that I can improve the performance speed of this SP. In the next couple of weeks I...
6
by: teedilo | last post by:
We have an application with a SQL Server 2000 back end that is fairly database intensive -- lots of fairly frequent queries, inserts, updates -- the gamut. The application does not make use of...
5
by: Scott | last post by:
I have a customer that had developed an Access97 application to track their business information. The application grew significantly and they used the Upsizing Wizard to move the tables to SQL...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
13
by: bjarne | last post by:
Willy Denoyette wrote; > ... it > was not the intention of StrousTrup to the achieve the level of efficiency > of C when he invented C++, ... Ahmmm. It was my aim to match the performance...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
7
by: Michael D. Ober | last post by:
When calling Enqueue, the internal array may need to be reallocated. My question is by how much? In the old MFC array classes, you could tell MFC how many additional elements to add to the array...
1
by: jvn | last post by:
I am experiencing a particular problem with performance counters. I have created a set of classes, that uses System.Diagnostics.PerformanceCounter to increment custom performance counters (using...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.