473,395 Members | 1,452 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,395 software developers and data experts.

which one is faster/better > or >=

Hi

This seems like a crazy question but I wanted to find out if anyone
knows which one of those two conditions are better (faster, smaller
memory footprint, can be optimized by the compiler, etc..)

1- if(i 0) {// do stuff}
2- if(i >=1) {// do stuff}

Thanks.

--
Mahdi.
Jan 21 '08 #1
11 1237
Mahdi wrote:
Hi

This seems like a crazy question but I wanted to find out if anyone
knows which one of those two conditions are better (faster, smaller
memory footprint, can be optimized by the compiler, etc..)

1- if(i 0) {// do stuff}
2- if(i >=1) {// do stuff}
They both get jitted to the same x86 code on my machine.

public static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
if (i 0) i++;
if (i >= 1) i++;
}
}

The code in the middle becomes:

if (i 0) i++;
0000001a test esi,esi
0000001c jle 0000001F
0000001e inc esi
if (i >= 1) i++;
0000001f test esi,esi
00000021 jle 00000024
00000023 inc esi

Alun Harford
Jan 21 '08 #2
They execute in the same time.
Jan 21 '08 #3
Mahdi wrote:
Hi

This seems like a crazy question but I wanted to find out if anyone
knows which one of those two conditions are better (faster, smaller
memory footprint, can be optimized by the compiler, etc..)

1- if(i 0) {// do stuff}
2- if(i >=1) {// do stuff}
There should absolutely be no difference.

--
Rudy Velthuis http://rvelthuis.de

"Quotation confesses inferiority." -- Ralph Waldo Emerson
Jan 21 '08 #4
Of course. But in some cases you need all the speed you can get and have
to sacrifice some readability. I know the situations would be rare but
they still exist where every bit of speed is required.
"Rare" is an understatement. I'd challenge anyone to show me a real-world
Windows program where slicing and dicing a few nanoseconds is going to make
any appreciable difference even if you could pull it off. The ambient
temperature of the room probably has a greater impact on performance.
Jan 22 '08 #5
Mahdi wrote:
This seems like a crazy question but I wanted to find out if anyone
knows which one of those two conditions are better (faster, smaller
memory footprint, can be optimized by the compiler, etc..)

1- if(i 0) {// do stuff}
2- if(i >=1) {// do stuff}
I agree with the others that have suggested to spend your time
on something more productive.

I will just add one additional note: you can measure current
..NET version on current CPU's. But there are not guarantee that
the results will stay valid forever.

Arne
Jan 22 '08 #6
"Larry Smith" <no_spam@_nospam.comwrote in message
news:ef**************@TK2MSFTNGP03.phx.gbl...
"Rare" is an understatement. I'd challenge anyone to show me a real-world
Windows program where slicing and dicing a few nanoseconds is going to
make any appreciable difference even if you could pull it off. The ambient
temperature of the room probably has a greater impact on performance.
It's not that rare. Any algorithm that loads all its data at the start, does
some lengthy processing, and saves the results at the end will benefit. I
have four such algorithms in my app.

Michael
Jan 22 '08 #7
>"Rare" is an understatement. I'd challenge anyone to show me a real-world
>Windows program where slicing and dicing a few nanoseconds is going to
make any appreciable difference even if you could pull it off. The
ambient temperature of the room probably has a greater impact on
performance.

It's not that rare. Any algorithm that loads all its data at the start,
does some lengthy processing, and saves the results at the end will
benefit. I have four such algorithms in my app.
This type of hairsplitting won't benefit you in any way you'll ever notice.
From a performance perspective, it's a complete waste of time even thinking
about it.
Jan 22 '08 #8
"NvrBst" <nv****@gmail.comwrote in message
news:cd**********************************@h11g2000 prf.googlegroups.com...
Basically I have a question very simular to the first; is doing this
type of thing (== to < && >) faster for C# (C# is what I mainly use
now)? Or just an urban myth of the past? Its always been something
in the back of my mind that I've been curious about but never got
around to actually testing myself.
My guess is it would be slower by a small amount but you never know. Maybe
on some processor a < is faster than =. The only way to know is try it.

Michael
Jan 23 '08 #9
"Larry Smith" <no_spam@_nospam.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
This type of hairsplitting won't benefit you in any way you'll ever
notice. From a performance perspective, it's a complete waste of time even
thinking about it.
One thing I wanted to add was that if creating a fast algorithm then
something like this would just be one tool in the toolchest. It might gain a
1% advantage but in combination with other optimisations it could give real
benefits.

Michael
Jan 24 '08 #10


"Michael C" <mi**@nospam.comwrote in message
news:OV**************@TK2MSFTNGP03.phx.gbl:
"Larry Smith" <no_spam@_nospam.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
This type of hairsplitting won't benefit you in any way you'll ever
notice. From a performance perspective, it's a complete waste of time
even
thinking about it.


One thing I wanted to add was that if creating a fast algorithm then
something like this would just be one tool in the toolchest. It might
gain a
1% advantage but in combination with other optimisations it could give
real
benefits.

Michael
1% ??? It must be the most trivial algorithm of all time.
or
You meant .00001%
Jan 25 '08 #11
"Ian Semmel" <an****@rocketcomp.com.auwrote in message
news:OC**************@TK2MSFTNGP03.phx.gbl...
1% ??? It must be the most trivial algorithm of all time.
or
You meant .00001%
Not necessarily. It just needs to be in a loop that doesn't have a lot of
code in that particular loop. There could be loops within loops within
loops. The inner most loop might be very trivial but that does not mean the
algorithm is. The other thing is the loop might well be extremely trivial,
such as flipping some bits in a bitmap.

Michael
Jan 25 '08 #12

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

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
11
by: Jamie Burns | last post by:
Hello, I just did a simple benchmark: for (xx=0;xx<100000;xx++) { rDerived* derived = dynamic_cast<rDerived*>(object); if (derived) derived->setValue(message.data.messageSetInt.value); } ...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
2
by: Russell Hind | last post by:
I have a delegate which I use to store a current 'state' function (for a statemachine inside a form). __delegate void State_t(const Message_c& Message); I assign to it such as m_State = new...
11
by: Farel | last post by:
Which is Faster in Python and Why? jc = {}; m = x = ,,.......] # upwards of 10000 entries def mcountb(): for item in x: b = item; b.sort(); bc = 0 for bitem in b: bc += int(bitem) try: m...
16
by: John Salerno | last post by:
My initial feeling is that concatenation might take longer than substitution, but that it is also easier to read: def p(self, paragraph): self.source += '<p>' + paragraph + '</p>\n\n' vs. ...
8
by: Sing | last post by:
Dear C Gurus, I would like to optimise a max() algo that my program uses many times. Which one is faster or are they the same? 1. #define max(a,b) (((a) (b)) ? (a) : (b)) 2. if (a>b) return a...
2
theGeek
by: theGeek | last post by:
I always wonder which one of join or subquery should I be using to solve a particuar problem so that I get the correct result set in minimum time. It usually happens that I can write a query quickly...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.