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

Result of Comparision

Execution of the following simple code results in variable z being
assigned the value of 1 ...

x = 234;
y = 234;
z = (x == y);

.... but I wanted z to be 234. What is the most efficient method (min
CPU cycles) of doing this? (I realize it could be accomplished as
follows)

if (x == y){
z = x;
)
else {
z = 0;
}

or

z = (x == y) ? x : 0; // Does this generate less machine code?

Nov 14 '05 #1
9 1453
I guess, what I may be asking is, if CPU's accumA has value1 and accumB
has value2, is there one instruction that will compare the two values
and set accumA to either 0 or leave accumA with original value1?

Nov 14 '05 #2
"jaym1212" <ja******@hotmail.com> writes:
Execution of the following simple code results in variable z being
assigned the value of 1 ...

x = 234;
y = 234;
z = (x == y);
Of course it does. The "==" operator yields 1 if its operands are
equal, 0 if they aren't.
... but I wanted z to be 234. What is the most efficient method (min
CPU cycles) of doing this?
C says nothing about performance or CPU cycles. The efficiency of a
given piece of code is implementation-dependent.
(I realize it could be accomplished as follows)

if (x == y){
z = x;
)
else {
z = 0;
}

or

z = (x == y) ? x : 0; // Does this generate less machine code? [indentation de-Googled]

The above could very well result in exactly the same machine code.
It's straightforward enough that the compiler is going to be able to
whatever optimization is possible; you're not likely to get any
performance increase by tweaking the source code. Use the highest
optimization level in your compiler.

(Incidentally, you should think about what result you want if x and y
are both equal to 0.)

In a followup, you wrote: I guess, what I may be asking is, if CPU's accumA has value1 and accumB
has value2, is there one instruction that will compare the two values
and set accumA to either 0 or leave accumA with original value1?


We have no idea. That's a question about your CPU, not about the C
language. There's no single operator in C that will do this.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
In article <11**********************@z14g2000cwz.googlegroups .com>
jaym1212 <ja******@hotmail.com> wrote:
Execution of the following simple code results in variable z being
assigned the value of 1 ...

x = 234;
y = 234;
z = (x == y);

... but I wanted z to be 234. What is the most efficient method (min
CPU cycles) of doing this?
Aside from its original design (where Dennis wanted a language that
was simple and clean and yet "close to the hardware"), C does not
really concern itself with efficiency.
z = (x == y) ? x : 0; // Does this generate less machine code?


I would use this expression -- which is I think the most straightforward
C language expression of your desired result -- and unless (a) the
program runs too slowly and (b) profiling shows that all the CPU
time is spent executing this particular line of code, not worry
about it.

If it *does* turn out that your program takes 600 hours to run, of
which 599 hours 58 minutes are spent on this line of code, you can
then rewrite the line in assembly, having obtained the CPU's
assembly-language manuals and appropriate timing information. You
might find that the C compiler has already produced optimal code,
or you might replace it with, e.g.:

sete r4,r6,r7 # r4 holds z, r6 and r7 hold x and y
neg r4 # 0 to 0, or 1 to -1
and r4,r6 # now z = r6 if r6 = r7, else z = 0

if those are the fastest instructions.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4
jaym1212 wrote:

Execution of the following simple code results in variable z being
assigned the value of 1 ...

x = 234;
y = 234;
z = (x == y);

... but I wanted z to be 234. What is the most efficient method (min
CPU cycles) of doing this? (I realize it could be accomplished as
follows)


Who knows. Try various methods and measure them on your system and
measure them. The language specifies nothing about speed and
efficiency. One further method you might consider is:

z = (x == y) * x;

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5
jaym1212 wrote:
Execution of the following simple code results in variable z being
assigned the value of 1 ...

x = 234;
y = 234;
z = (x == y);
x==y does a comparison whose result is 1 as both its arguments are
equal, this is what gets stored in z.
... but I wanted z to be 234. What is the most efficient method (min
CPU cycles) of doing this? (I realize it could be accomplished as
follows)

if (x == y){
z = x;
)
else {
z = 0;
}

or

z = (x == y) ? x : 0; // Does this generate less machine code?


I'd use this as this uses less code and is fairly simple and
explanatory.

Efficiency, C language does not guarantee efficiency but it does
guarantee, under bounded condition where you do not enter the realm of
undefinied beaviour, efficacy in what it's result/final output would
be. Efficiency would depend on how the compiler compiles the source.
For various targets the time of execution for the same block might be
different. Even for same target if you use a simple compiler there
might not be mcuh time gain, but a good one and also a costly :) one
would do all sorts of funny stuff, called optimizations, pipelining
and wot not so that final effect is of reduced time of execution.
HTH.
Regards,
Taran (TT)

Nov 14 '05 #6
On 20 Jan 2005 15:27:37 -0800, jaym1212
<ja******@hotmail.com> wrote:
I guess, what I may be asking is, if CPU's accumA has value1 and accumB
has value2, is there one instruction that will compare the two values
and set accumA to either 0 or leave accumA with original value1?


Yes, on at least one CPU there is such an instruction (clear
accumulator, and any instruction can be conditional). However, whether
you use such a CPU I don't know, and whether any C compiler would
optimise any particular construct to use it I don't know either, and it
is irrelevant to the C language which don't have a specific construct to
do it. I would use a = (a == b ? a : 0) and let the compiler determine
the optimal code for whatever processor it is targetting. Or if you
want to be tricky (and probably slower):

a *= (a == b);

would do the same (a relational operator returns 1 if true and 0 if
false).

But premature optimisation is almost always a bad idea, and it can
result in worse code on some platforms (hence the 'register' keyword
being deprecated, it says to the compiler "I want this variable in a
register" but that may well not be the best one to optimise). Better to
write your code in a clear fashion, and only if you find it taking too
long run a profiler and find which parts are actually taking the time
(and be prepared to be very surprised, in the (uhm!) years I've been
programming I've seen lots of people very surprised that the
inefficiencies weren't where they expected and that some code they
thought was bound to be inefficient was optimised well by that
particular compiler).

Chris C
Nov 14 '05 #7
In article <11**********************@z14g2000cwz.googlegroups .com>,
Taran <ta************@honeywell.com> wrote:
jaym1212 wrote: <SNIP>
CPU cycles) of doing this? (I realize it could be accomplished as <SNIP> z = (x == y) ? x : 0; // Does this generate less machine code?


I'd use this as this uses less code and is fairly simple and
explanatory.


Seconded.

Efficiency, <SNIP>

If it is easy for you to understand, it is easy for the compiler
writers, and for the compiler.
So if you want efficiency, write clear code.
(And yes I know this is off-topic.)

The last time I tried to help the compiler was in 1985.
I had a sorting problem, and the comparision routine was
all in one function carefully laid out to help the compiler
in optimising. Indeed it did. I couldn't shave off one
cycle off the output of compiler inspecting the assembly
listing. (VAX dec assembly, the c-compiler was indeed good.)

Then it turned out that sorting was completely io-bound.
Worse even, the requirement that a certain type of files
was to be used, made the sorting an order of magnitude
slower, beyond my control.

Regards,
Taran (TT)


--
Groetjes Albert
--
Albert van der Horst,Oranjestr 8,3511 RA UTRECHT,THE NETHERLANDS
One man-hour to invent,
One man-week to implement,
One lawyer-year to patent.
Nov 14 '05 #8
On 20 Jan 2005 23:54:20 GMT, Chris Torek <no****@torek.net> wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>
jaym1212 <ja******@hotmail.com> wrote:
Execution of the following simple code results in variable z being
assigned the value of 1 ...

x = 234;
y = 234;
z = (x == y);

... but I wanted z to be 234. What is the most efficient method (min
CPU cycles) of doing this?
Aside from its original design (where Dennis wanted a language that
was simple and clean and yet "close to the hardware"), C does not
really concern itself with efficiency.
z = (x == y) ? x : 0; // Does this generate less machine code?


I would use this expression -- which is I think the most straightforward
C language expression of your desired result -- and unless (a) the
program runs too slowly and (b) profiling shows that all the CPU
time is spent executing this particular line of code, not worry
about it.

If it *does* turn out that your program takes 600 hours to run, of
which 599 hours 58 minutes are spent on this line of code, you can
then rewrite the line in assembly, having obtained the CPU's
assembly-language manuals and appropriate timing information. You
might find that the C compiler has already produced optimal code,
or you might replace it with, e.g.:


z = (x == y) ? x : 0;
sete r4,r6,r7 # r4 holds z, r6 and r7 hold x and y
what does it mean "sete r4,r6,r7"? (r4=r6=r7? r4=(r6==r7)?)
I think the second
neg r4 # 0 to 0, or 1 to -1
and r4,r6 # now z = r6 if r6 = r7, else z = 0

if those are the fastest instructions.

Nov 14 '05 #9
On Sat, 29 Jan 2005 18:58:08 +0000, RoSsIaCrIiLoIA wrote:

....
z = (x == y) ? x : 0;
sete r4,r6,r7 # r4 holds z, r6 and r7 hold x and y
what does it mean "sete r4,r6,r7"? (r4=r6=r7? r4=(r6==r7)?)
I think the second


The second would make sense in the context.
neg r4 # 0 to 0, or 1 to -1
and r4,r6 # now z = r6 if r6 = r7, else z = 0

if those are the fastest instructions.


Lawrence

Nov 14 '05 #10

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

Similar topics

33
by: Axel Boldt | last post by:
Hello, is there a rationale for the following behavior: >>> a = (1,2) >>> b = (1,2) >>> a is b False >>> a = "12" >>> b = "12"
2
by: Florian Lindner | last post by:
Hello, I've read the chapter in the Python documentation, but I'm interested in a a more in-depth comparision. Especially regarding how pythonic it is and how well it performs and looks under...
1
by: John Black | last post by:
Hi, In using find_if() you need to name a comparision function which normally is a static function, but sometimes it is really inconvinient for this, let's take this example, class MyClass{...
2
by: Ashwin Kambli | last post by:
Hi, I am doing a study on the performance comparision of C# and Java. Links to any articles on this topic will be greatly appreciated. Thanking you, Ashwin
10
by: Viktor Popov | last post by:
Hi , I have the following SP: CREATE PROCEDURE prSVUSERINFO @USRNAM VARCHAR(20), @USRPSW VARCHAR(20), @NAME VARCHAR(64), @ADDR VARCHAR(74), @EML VARCHAR(64), @ROLE VARCHAR(6), @RCOUNT INT...
3
by: kd | last post by:
Hi All, How to perform case-insensitive comparision of strings? Would there be some kind of an indicator, which when set to true, would allow case-insenitive comparision of strings using...
2
by: nirav.lulla | last post by:
I have been given the task to come up with Requirements, Comparision and Migration document from Shadow Direct to DB2 Connect. I am very new much to all this, but kind of know a little bit about...
3
by: abctech | last post by:
I have an Html page, user enters a Date (dd-mm-yyyy) here. There's a servlet connected in the backend for processing this submitted information, it must have a method to compare this entered date...
10
atksamy
by: atksamy | last post by:
HI, I am trying to compare a series of strings like the following rodeo rodas carrot crate GLX GLX 1.1 GLX glxs
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.