Solved

Which math operator is faster?

Posted on 2012-03-17
4
395 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 82

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 82

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now