Result of Comparision

J

jaym1212

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?
 
J

jaym1212

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?
 
K

Keith Thompson

jaym1212 said:
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.)

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.
 
C

Chris Torek

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.
 
C

CBFalconer

jaym1212 said:
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;
 
T

Taran

jaym1212 said:
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)
 
C

Chris Croughton

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
 
A

Albert van der Horst

jaym1212 wrote:

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.
 
R

RoSsIaCrIiLoIA

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.


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
 
L

Lawrence Kirby

On Sat, 29 Jan 2005 18:58:08 +0000, RoSsIaCrIiLoIA wrote:

....
z = (x == y) ? x : 0;


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.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top