444,058 Members | 1,213 Online
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

# Result of Comparision

 P: n/a 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 Replies

 P: n/a 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

 P: n/a "jaym1212" 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 San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Nov 14 '05 #3

 P: n/a In article <11**********************@z14g2000cwz.googlegroups .com> jaym1212 wrote:Execution of the following simple code results in variable z beingassigned the value of 1 ...x = 234;y = 234;z = (x == y);... but I wanted z to be 234. What is the most efficient method (minCPU 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

 P: n/a 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

 P: n/a 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

 P: n/a On 20 Jan 2005 15:27:37 -0800, jaym1212 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

 P: n/a In article <11**********************@z14g2000cwz.googlegroups .com>, Taran wrote:jaym1212 wrote: CPU cycles) of doing this? (I realize it could be accomplished as z = (x == y) ? x : 0; // Does this generate less machine code?I'd use this as this uses less code and is fairly simple andexplanatory. Seconded. Efficiency, 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

 P: n/a On 20 Jan 2005 23:54:20 GMT, Chris Torek wrote:In article <11**********************@z14g2000cwz.googlegroups .com>jaym1212 wrote:Execution of the following simple code results in variable z beingassigned the value of 1 ...x = 234;y = 234;z = (x == y);... but I wanted z to be 234. What is the most efficient method (minCPU cycles) of doing this?Aside from its original design (where Dennis wanted a language thatwas simple and clean and yet "close to the hardware"), C does notreally 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 straightforwardC language expression of your desired result -- and unless (a) theprogram runs too slowly and (b) profiling shows that all the CPUtime is spent executing this particular line of code, not worryabout it.If it *does* turn out that your program takes 600 hours to run, ofwhich 599 hours 58 minutes are spent on this line of code, you canthen rewrite the line in assembly, having obtained the CPU'sassembly-language manuals and appropriate timing information. Youmight 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 = 0if those are the fastest instructions. Nov 14 '05 #9

 P: n/a 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 = 0if those are the fastest instructions. Lawrence Nov 14 '05 #10

### This discussion thread is closed

Replies have been disabled for this discussion.