Solved

Which math operator is faster?

Posted on 2012-03-17
4
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 51

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

630 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