473,545 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Greater than operator Vs Equal to operator : Which one is efficient?

Hello All,

Which one is an expensive operation?
'>' or '==" ?

I know this won't matter much. However, if I'm executing this
operation million times, I would prefer the better choice.

My gut feeling is that '>' should be efficient. However, when
translated to assembly code all it generates is "cmpl" instruction
(gcc -S <prog.c>). So, my question here is :: how is "cmpl"
instruction executed by the cpu?

Ex: (1) if (n == 0) vs if (n > 0)
(2) if (len == 12) vs if (len < 13)

Thanks in advance.

Regards,
Vivek

web: http://students.iiit.net/~vivek/
Nov 13 '05 #1
36 4569
hy********@yaho o.com (Vivek Mandava) writes:
Which one is an expensive operation?
'>' or '==" ?
The C standard doesn't define the performance of these operations.
I know this won't matter much. However, if I'm executing this
operation million times, I would prefer the better choice.

My gut feeling is that '>' should be efficient. However, when
translated to assembly code all it generates is "cmpl" instruction
(gcc -S <prog.c>). So, my question here is :: how is "cmpl"
instruction executed by the cpu?


This is not a C question, but a question about assembler/machine code
on a specific platform. It is off-topic here; please ask in a newsgroup
dedicated to programming the CPU in question.

Martin
Nov 13 '05 #2
On Fri, 05 Sep 2003 01:25:30 -0700, Vivek Mandava wrote:
Hello All,

Which one is an expensive operation?


That would depend on both plattform and compiler I think.

int main()
int i,j;

i=INT_MAX;
j=INT_MAX;

print_system_ti me();

while(1) {
if (i == 0) {
break;
} else {
++i;
}
}

print_system_ti me();

while(1) {
if (j < 1) {
break;
} else {
++j;
}
}

print_system_ti me;

return 0;
}

hth
--
NPV
"Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink

Nov 13 '05 #3

"Vivek Mandava" <hy********@yah oo.com> wrote in message
news:bb******** *************** ***@posting.goo gle.com...
Hello All,

Which one is an expensive operation?
'>' or '==" ?

I know this won't matter much. However, if I'm executing this
operation million times, I would prefer the better choice.

My gut feeling is that '>' should be efficient. However, when
translated to assembly code all it generates is "cmpl" instruction
(gcc -S <prog.c>). So, my question here is :: how is "cmpl"
instruction executed by the cpu?


Why don't ask in your platform specific newsgroup ?
--
Jeff
Nov 13 '05 #4
Vivek Mandava wrote:
Which one is an expensive operation?
'>' or '==" ?


That's highly system-dependent and therefore OT here. That said, I'd
be surprised if there were any difference. It should use some kind
of "compare" instruction either way.

Why not put a tight loop of 10^7 of them in a function and run your
profiler on it?

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk. pbz' | rot13
Nov 13 '05 #5
hy********@yaho o.com (Vivek Mandava) writes:
Which one is an expensive operation?
'>' or '==" ?
The C standard doesn't define the performance of these operations.
I know this won't matter much. However, if I'm executing this
operation million times, I would prefer the better choice.

My gut feeling is that '>' should be efficient. However, when
translated to assembly code all it generates is "cmpl" instruction
(gcc -S <prog.c>). So, my question here is :: how is "cmpl"
instruction executed by the cpu?


This is not a C question, but a question about assembler/machine code
on a specific platform. It is off-topic here; please ask in a newsgroup
dedicated to programming the CPU in question.

Martin
Nov 13 '05 #6
hy********@yaho o.com (Vivek Mandava) wrote in message news:<bb******* *************** ****@posting.go ogle.com>...
Hello All,

Which one is an expensive operation?
'>' or '==" ?

That's entirely a function of the target instruction set, not the C
language.
I know this won't matter much. However, if I'm executing this
operation million times, I would prefer the better choice.

The only way to know for sure is to write code using each operator and
gather performance statistics on that code, recognizing that your
results are only valid for that particular architecture. FWIW,
they're equally expensive on all the platforms I'm familiar with (they
all generate similar instructions). I'd honestly be surprised if
there was a platform where one was significantly more expensive than
the other.
My gut feeling is that '>' should be efficient. However, when
translated to assembly code all it generates is "cmpl" instruction
(gcc -S <prog.c>). So, my question here is :: how is "cmpl"
instruction executed by the cpu?

Assuming you're on x86:
http://www.penguin.cz/~literakl/intel/intel.html
Ex: (1) if (n == 0) vs if (n > 0)
(2) if (len == 12) vs if (len < 13)

Thanks in advance.

Regards,
Vivek

web: http://students.iiit.net/~vivek/

Nov 13 '05 #7
On Fri, 05 Sep 2003 01:25:30 -0700, Vivek Mandava wrote:
Hello All,

Which one is an expensive operation?


That would depend on both plattform and compiler I think.

int main()
int i,j;

i=INT_MAX;
j=INT_MAX;

print_system_ti me();

while(1) {
if (i == 0) {
break;
} else {
++i;
}
}

print_system_ti me();

while(1) {
if (j < 1) {
break;
} else {
++j;
}
}

print_system_ti me;

return 0;
}

hth
--
NPV
"Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink

Nov 13 '05 #8

"Vivek Mandava" <hy********@yah oo.com> wrote in message
news:bb******** *************** ***@posting.goo gle.com...
Hello All,

Which one is an expensive operation?
'>' or '==" ?

I know this won't matter much. However, if I'm executing this
operation million times, I would prefer the better choice.

My gut feeling is that '>' should be efficient. However, when
translated to assembly code all it generates is "cmpl" instruction
(gcc -S <prog.c>). So, my question here is :: how is "cmpl"
instruction executed by the cpu?


Why don't ask in your platform specific newsgroup ?
--
Jeff
Nov 13 '05 #9
Vivek Mandava wrote:
Which one is the more expensive operation?

'>' or '==' ?

I know this won't matter much.
However, if I'm executing this operation million times,
I would prefer the better choice.

My gut feeling is that '>' should be efficient.
However, when translated to assembly code
all it generates is "cmpl" instruction (gcc -S <prog.c>).
So, my question here is,
"How is the 'cmpl' instruction executed by the cpu?"

Ex: (1) if (n == 0) vs if (n > 0)
(2) if (len == 12) vs if (len < 13)


The cmpl instruction is a subtraction which does *not* save a result.
It just sets some status register flags.
The branch instructions (je, jne, jlt, jle, jgt, jge, etc.)
test these status bits and branch accordingly.
None of them are inherently faster than the others.
Your CPU almost certainly includes branch prediction hardware
which it can use to prefetch instructions for the most likely branch.
Your compiler almost certainly includes optimizations
to assist branch prediction.

It is almost always a bad idea to attempt to outsmart
your optimizing compiler.
You will probably only succeed in frustrating these optimizations.
Write your code in the most straight forward and readable way
that you can and let your optimizing compiler figure out
how to generate instructions that yield the highest performance
and efficiency. If performance is not satisfactory,
use a profiler to locate the code that produces the bottleneck
and concentrate on optimizing that code first.

Nov 13 '05 #10

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

Similar topics

3
8073
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h =================================================================================== //class bigInt implements big integers by storing the digits of a big //integer in a private vector data member...
3
2208
by: Massimiliano Alberti | last post by:
Can someone check this? If it's OK, you can use however you want... :-) It should search for an element in an array and, if it can't find it, return the next element. key is what you are searching, in a *base array of LONG with num elements. After some tests it seems to work well (the original version was filled with ASSERTs) (I will use...
11
2367
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal state\n"), TRUE) : \ (state = newstate, FALSE) This macro is called like this: setState(state, ST_Used);
2
4574
by: David Laub | last post by:
I know there is no C# exponentiation operator. But since the double class is sealed, there seems no way to add the operator override without creating a new class which uses containment (of a double value) This seems a major pain, and would probably wind up being more syntactically messy than just calling Math.Pow(x,y) Surely greater...
0
980
by: Vinay Jain | last post by:
query like: select distinct name from student; uses equal operator ('=') or not? If not, then how it identifies distinct element. -- Vinay Jain Dissertation Project Trainee DAKE Division C-DAC Mumbai
2
9774
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator double() of class B is called for the member complex::operator*=(double) as expected, but not for operator*(complex, double). The effect is, that the...
5
2647
by: huan | last post by:
int *p=new int(7) delete p; could be written as void* buf=operator new(sizeof(int)); int* p=new (buf) int(7); operator delete(p); My question is how to use operator new, placement new to allocate and initialize the storage and use operator delete to deallocate the storage?
11
4127
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my mind) and it would be fairly generic. The only sidecase I can see is that operator. itself would not be looked up through operator. . I read that...
3
2148
by: hurcan solter | last post by:
I have an host class that holds fundamental types template<typename T> struct Generic{ Generic(T val= T()):mval(val){} operator T(){return mval;) T mval; }
9
2510
by: Ioannis Vranos | last post by:
Well when we have an expression like this: int obj= first | second; Why is this style preferred than the equivalent: int obj= first+ second; ?
0
7468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7757
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5329
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.