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

which one will execute faster flag==0 or 0==flag and why?

if of the following will execute faster
Expand|Select|Wrap|Line Numbers
  1. if ( flag == 0) 
  2. {
  3. }
  4.  
or
Expand|Select|Wrap|Line Numbers
  1. if ( 0 == flag) 
  2. {
  3. }
  4.  
Oct 20 '10 #1
2 1906
code green
1,726 Expert 1GB
insignificant I would say.
But I bet if(!flag) is faster still.
Oct 20 '10 #2
Oralloy
988 Expert 512MB
It all depends on what assembly code the compiler generates. Essentially, do "0 == flag" and "flag == 0" compile down to the same code, or not?

Then there are the questions of how the system executes the assembly language level comparison command. Various computers will behave differently, depending. On a PDP-11 or VAX, the entire test is boiled down to one memory instruction which may be 10 or 12 bytes long, depending on the "addressing mode" used for the operands. Who knows if loading an immediate "0" followed by a memory fetch to a stack location is faster than the other way around. Or perhaps it's one simple test instruction, depending on the command set available.

Lastly ... a really good optimizer will eliminate the whole code sequence, as there is nothing to do as the consequent of the test. In this case, execution time for the "if" statement is exactly zero.

Cheers! :)
Oralloy
Oct 20 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Sara Khalatbari | last post by:
Dear friends In a code, I'm opening a file to read. Like : lines = open(filename).readlines() & I'm never closing it. I'm not writing in that file, I just read it. Will it cause any problems...
1
by: JT | last post by:
i hope no one is getting sick of my server.execute/transfer questions.. does server.execute return a value? im wondering what happens if an error occurs in the .asp file that i'm calling with...
0
by: amit | last post by:
hi I have created a tool which does a find and replace thru DTE, now after it is done, it opens up a window, "FIND REACHED THE STARTING POINT OF SEARCH" I want to disbale this window...
3
by: Jim Archer | last post by:
Hi All... I'm been fighting this problem for a few days now, and it seems like it should be simple. But the solution has eluded me so far... I need to flag a record when it is updated or when...
1
by: rpapaiof | last post by:
is it possible to write the same program wit same commands without flag and also please describe flag #include <stdio.h> int main(void){ int j,n,i,flag; printf("Please enter a number :...
9
by: PengYu.UT | last post by:
Hi, I feel argparse has some useful things that optparse doesn't have. But I can't find it argparse in python library reference. I'm wondering when it will be available in the python standard...
5
by: nimitsis | last post by:
Hello to everyone I wont to create 2 threads in Python ( thread01, thread02 ), which doing the follows : thread01, ask user to give a value to flag variable and write it to the shared memory of...
80
by: nicolas.sitbon | last post by:
Hi everybody, in a french C book, the author says that only {fgetc, getc, getchar, fgetwc, getwc, getwchar, fgets, gets, fgetws, getws, fputc, putc, putchar, fputwc, putwc, putwchar, fputs, puts,...
37
by: C_guy | last post by:
Does anyone know of a (hopefully free) tool that can traverse a project and determine which "#include"s are not needed or needed in every .C file? This would be helpful in removing header...
3
by: SwapnilD | last post by:
I am creating a feature which will allow users to import data of excel sheet to database. First, data will be imported from excel sheet and will be displayed in a tabular format. User performs...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...
0
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...

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.