Solved

Which math operator is faster?

Posted on 2012-03-17
4
398 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 49

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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