Re: How Math.Cos & Math.Sin is implemented?
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Tue, 17 Oct 2006 17:04:00 +0200
"Morgan Cheng" <morgan.chengmo@xxxxxxxxx> wrote in message
news:1161054230.103345.250540@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
| Willy Denoyette [MVP] wrote:
| > "Morgan Cheng" <morgan.chengmo@xxxxxxxxx> wrote in message
| > news:1160998378.369140.117010@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
| > |
| > | Willy Denoyette [MVP] wrote:
| > | > "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
| > | > news:MPG.1f9a8f987ae2c6b198d544@xxxxxxxxxxxxxxxxxxxxxxx
| > | > | Michael A. Covington <look@xxxxxxxxxxxxxxxxxxxxxx> wrote:
| > | > | > > In this case, a quick test on my laptop showed Math.Sin being
| > called
| > | > | > > 1,000,000,000 times in less than 2 seconds. Just how often is
your
| > | > | > > program going to call the trig methods?
| > | > | >
| > | > | > With different arguments each time, or were most of the calls
| > optimized
| > | > | > away?
| > | > | >
| > | > | > That's 500 sine computations per microsecond. A microsecond is
| > maybe
| > | > 2400
| > | > | > clock cycles on your Pentium. I don't recall if the Pentium
clock
| > is
| > | > | > divided down. Even if it's not, 4.8 clock cycles per sine
| > computation
| > | > is
| > | > | > not quite credible.
| > | > |
| > | > | You're right. Here's a somewhat better test - I suspect things
were
| > | > | being optimised out before and I was too sleepy to notice. Oops!
| > | > |
| > | > | using System;
| > | > |
| > | > | class Test
| > | > | {
| > | > | static void Main()
| > | > | {
| > | > | double total = 0.23;
| > | > |
| > | > | DateTime start = DateTime.Now;
| > | > | for (int i=0; i < 100000000; i++)
| > | > | {
| > | > | total += Math.Sin(total);
| > | > | total += Math.Cos(total);
| > | > | }
| > | > | DateTime end = DateTime.Now;
| > | > |
| > | > | Console.WriteLine (end-start);
| > | > | Console.WriteLine (total);
| > | > | }
| > | > | }
| > | > |
| > | > | This is harder work, of course - 2 trig operations and 2 additions
per
| > | > | cycle. The timing on my box is 12 seconds for the 100,000,000
cycles.
| > | > | Not as fast as before, but still likely to be fast enough for the
OP
| > | > | not to have to worry :)
| > | >
| > | > Following is exactly what the JIT has produced from the loop in
release
| > mode
| > | > , the figures between () are the instruction latencies ( here for
AMD64,
| > | > your's may vary).
| > | >
| > | > dd0424 fld qword ptr [esp] (4)
| > | > d9fe fsin (93)
| > | > dc0424 fadd qword ptr [esp] (6)
| > | > dd1c24 fstp qword ptr [esp] (2)
| > | > dd0424 fld qword ptr [esp] (4)
| > | > d9ff fcos (92)
| > | > dc0424 fadd qword ptr [esp] (6)
| > | > dd1c24 fstp qword ptr [esp] (2)
| > | > 83c601 add esi,1 (1)
| > | > 81fe00e1f505 cmp esi,5F5E100h (4)
| > | > 7cda jl 00cb00a0 (1)
| > | >
| > | > that's a total 215 clock cycles per loop. On my box with a clock
cycle
| > of
| > | > ~0,4329 nSec. that would account for ~93 nSec per loop, or 9.3 sec.
for
| > | > 100.000.000.000. Actually the test runs in 8.59 sec. this because
there
| > is
| > | > some amount of // execution done.
| > | >
| > | Thanks for your clarification.
| > | I did some expriementation too. It shows sin/cos doesn't take much cpu
| > | cycles, but I still prefer to pre-compute needed sin/cos value in two
| > | array, and fetch them later. Since I am implementing Hough
| > | Transformation, which needs cos/sin in a X*Y loop(X & Y are image
width
| > | and height). Accessing an array is always supposed to be faster than
| > | Math.Cos & Math.Sin function call, right?
| >
| >
| > Could be, but keep in mind that using a table look-up might introduce
some
| > hidden costs.
| You mean cost introduced by array boundary check?
These are not hidden and are (really small) fixed costs, hidden costs are
things like L1/L2 data cache misses, hard to measure and unpredictable.
Using a lookup table will certainly speed-up the calculation, but large
tables will introduce cache misses and these might well be important enough
to reduce the performance gain considerably, so keep them as small as
possible, don't store doubles use floats instead.
Willy.
.
- References:
- How Math.Cos & Math.Sin is implemented?
- From: Morgan Cheng
- Re: How Math.Cos & Math.Sin is implemented?
- From: Jon Skeet [C# MVP]
- Re: How Math.Cos & Math.Sin is implemented?
- From: Michael A. Covington
- Re: How Math.Cos & Math.Sin is implemented?
- From: Jon Skeet [C# MVP]
- Re: How Math.Cos & Math.Sin is implemented?
- From: Willy Denoyette [MVP]
- Re: How Math.Cos & Math.Sin is implemented?
- From: Morgan Cheng
- Re: How Math.Cos & Math.Sin is implemented?
- From: Willy Denoyette [MVP]
- Re: How Math.Cos & Math.Sin is implemented?
- From: Morgan Cheng
- How Math.Cos & Math.Sin is implemented?
- Prev by Date: Re: App crashes in InitializeComponent();
- Next by Date: Re: In every time I pressing Ctrl + Shift + B, Visual Studio Compi
- Previous by thread: Re: How Math.Cos & Math.Sin is implemented?
- Next by thread: TreeView Nonsense
- Index(es):
Relevant Pages
|