Solved

Which math operator is faster?

Posted on 2012-03-17
4
399 Views
Last Modified: 2012-04-11
Which line should process faster and why?

if (x == y) { }

Open in new window

OR
if (x > y) { }

Open in new window

OR
if (x < y) { }

Open in new window

OR
if (x >= y) { }

Open in new window


Which operator should process faster (+, -, * or /)?
0
Comment
Question by:Mohamed Abowarda
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 133 total points
ID: 37734190
The 'if' lines should all be the same because subtraction is normally used and the results are used to determine the response.  The first checks for a 0 result, the second checks for a positive result, negative result, and positive or 0 result.

For the operators, + and - are the fastest for integers because they are a single step operation, * and / are multiple steps.  For floating point, they are all multiple steps.
0
 
LVL 12

Author Comment

by:Mohamed Abowarda
ID: 37734234
Can you give me an example on how the computer process +, -, *, and /?

I want detailed information.

Thanks,
0
 
LVL 50

Assisted Solution

by:Gustav Brock
Gustav Brock earned 67 total points
ID: 37734321
You've got it:

http://msdn.microsoft.com/en-us/library/aa309387(v=vs.71).aspx

That's for .Net 1.1. For 4.0 it is comes with the Microsoft Windows SDK Tools and is typically located in:

C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\ildasm.exe

/gustav
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 133 total points
ID: 37734324
For integers and characters that fit in the main CPU registers (32 or 64 bit), + and - can simply be done in a single step by the main ALU.  All other operations are passed to the math coprocessor.  This page http://www.top500.org/2007_overview_recent_supercomputers/intel_xeon gives a good example of the blocks in an Intel CPU.  An earlier CPU would be easier to understand since there isn't as much detail.  Here's one http://www.laynetworks.com/Block%20diagram%20of%20the%20Pentium.htm of an early Pentium CPU.   But they keep adding things to speed up the instructions.  Now all of this is hidden from you by the C# compiler and the .NET framework.  The compiler determines what kind of data you are using and selects the appropriate .NET CLR routines to process it.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 37
Need help converting bitmap to image in VB.Net 8 44
Unlocking a column in excel using C# 17 26
VB.Net  Exporting Excel Woorkbook to PDF 7 58
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question