Math.Cos& Math.Sin实现了吗? [英] How Math.Cos & Math.Sin is implemented?

查看:126
本文介绍了Math.Cos& Math.Sin实现了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在写一个程序,需要很多Math.Cos& Math.Sin

操作。我担心这会对性能产生影响。


任何人都知道Math.cos& Math.Sin实现了吗?

我想它只是检索一个巨大的预先计算的表,它可能是快速的b / b
。我试图在我自己的数组中缓存所有可能的角度cos / sin,它会更快地调用Math.Cos&数学。所有的时间。

解决方案



" Morgan Cheng" < mo ************ @ gmail.comwrote in message

news:11 ****************** ***@f16g2000cwb.googlegro ups.com ...





我正在写一个程序,需要大量的Math.Cos& Math.Sin

操作。我担心这会对性能产生影响。


任何人都知道Math.cos& Math.Sin实现了吗?

我想它只是检索一个巨大的预先计算的表,它可能是快速的b / b
。我试图在我自己的数组中缓存所有可能的角度cos / sin,它会更快地调用Math.Cos&数学。所有的时间。



它使用算法。表只产生有限的精度,并且需要

类似2 * Pi * 10 ^ 7的值才能从

良好算法中获得相同的浮点精度。您可以通过使用对称性来减小表格大小,但是当您这样做时最终会引入开销并且它仍然在

相同的订单上。


完全是您的选择。你选择一个速度和浪费记忆的表格,或者你使用算法来获得精确而不是内存。


2006年10月12日19: 59:05 -0700,Morgan Cheng

< mo ************ @ gmail.comwrote:


>任何人都知道Math.cos& Math.Sin实现了吗?



完全没有。也就是说,所有现代个人计算机CPU都有一个内置数学协处理器,直接提供三角函数
函数。 Math。*方法只是转发对这些优化的硬件设施的调用。因此,通过在C#甚至C ++中编写软件算法来获得更好的速度是极不可能的。

-
http://www.kynosarges.de


< blockquote> Morgan Cheng< mo ************ @ gmail.comwrote:


我正在编写一个需要花费很多时间的程序Math.Cos& Math.Sin

操作。我担心这会对性能产生影响。



每当你遇到性能恐惧时,运行测试。在大多数情况下,在我的

体验中,你会发现表现出对单个

代码的担忧是没有根据的。


在这种情况下,我的笔记本电脑上的快速测试表明,Math.Sin在不到2秒的时间内被称为
1,000,000,000次。您的

计划多久会调用一次触发方法?


-

Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ ~Sibet 博客: http://www.msmvps.com/jon。双向飞碟

如果回复小组,请不要给我发邮件


Hi,

I am writing a program that will take a lot of Math.Cos & Math.Sin
operation. I am afraid this will be source of performance impact.

Anybody knows how Math.cos & Math.Sin is implemented?
I suppose it just retrieving a huge pre-computed table, it might be
quick. I tried to cache all possible angle cos/sin in my own array , it
turns to be much faster to call Math.Cos & Math.Sin all the time.

解决方案


"Morgan Cheng" <mo************@gmail.comwrote in message
news:11*********************@f16g2000cwb.googlegro ups.com...

Hi,

I am writing a program that will take a lot of Math.Cos & Math.Sin
operation. I am afraid this will be source of performance impact.

Anybody knows how Math.cos & Math.Sin is implemented?
I suppose it just retrieving a huge pre-computed table, it might be
quick. I tried to cache all possible angle cos/sin in my own array , it
turns to be much faster to call Math.Cos & Math.Sin all the time.

It uses an algorithm. Tables only produce finite precision and it takes
something like 2*Pi*10^7 values to get the same precision of a float from a
good algorithm. You can reduce the table size by using symmetry and such but
you end up introducing overhead when you do that too and its still on the
same order.

Utlimately its your choice. You choose a table for speed and waste memory or
you use an algorithm for precision and not wste memory.


On 12 Oct 2006 19:59:05 -0700, "Morgan Cheng"
<mo************@gmail.comwrote:

>Anybody knows how Math.cos & Math.Sin is implemented?

Not at all. That is, all modern personal computer CPUs have a
built-in math coprocessor that directly provides trigonometric
functions. The Math.* methods simply forwards calls to these
optimized hardware facilities. So it''s extremely unlikely that you''ll
get better speed by writing a software algorithm in C# or even C++.
--
http://www.kynosarges.de


Morgan Cheng <mo************@gmail.comwrote:

I am writing a program that will take a lot of Math.Cos & Math.Sin
operation. I am afraid this will be source of performance impact.

Whenever you have performance fears, run tests. Most of the time, in my
experience, you''ll find that performance fears about individual bits of
code are unfounded.

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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于Math.Cos&amp; Math.Sin实现了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆