|
| Willy Denoyette [MVP] 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.