开发者

Does Math.Sqrt cache its own results?

Someone has suggested to me that the built in C# Math.Sqrt 开发者_如何学Gofunction in .NET 4.0 caches its results, so that if I call Math.Sqrt(50) over and over again, it's not actually doing a sqrt, but just pulling the answer from a cache. Can anyone verify or deny this claim? If it's true then I have a bunch of needless caching going on in my code.


I don't belive so.

You can try and do a benchmark, call Math.Sqrt on 1000.000 different numbers and then on 1000.000 same numbers, the time taken should be similar.

May you can have a look into reflector which reveals you (at least the unmanaged parts) of the Math.Sqrt function code.

If you need a cache, you should create one on your own with some kind of hashtable. Note that you should leave an epsilon, that is for example 1.0000000 should be considered the same as 1.00000001, otherwise you get into trouble with accuracy.


i don't have .net 4 installed but you can vrify this easily by viewing the said method in reflector.
this is the only way that you'll be sure to see what's going on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜