473,322 Members | 1,473 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.

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 1701

"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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.