简体   繁体   English

缓存java.lang.Double

[英]caching of java.lang.Double

I have lots of Doubles in my program (notice the capital D) and I was thinking of a possible caching mechanism. 我的程序中有很多Doubles(注意大写D),我在考虑一种可能的缓存机制。

By looking at the Double constructors / factories, none is actually implementing a "cache" (like the way Integer, works, for example when using valueOf ). 通过查看Double构造函数/工厂,实际上没有一个实现“缓存”(例如Integer的工作方式,例如,在使用valueOf )。

I was thinking to implement such a cache by using a HashMap<String,Double> where the key is the string representation. 我当时正在考虑通过使用HashMap<String,Double>来实现这样的缓存,其中键是字符串表示形式。

Any visible drawbacks / cons of such implementation? 这样实施有什么明显的缺点/缺点吗?

I wonder why this wasn't done or provided yet by Java. 我不知道为什么Java尚未完成或提供此功能。

Your problem is that any possible caching mechanism will use far more memory and processor power than you are ever likely to save. 您的问题在于,任何可能的缓存机制都将使用比您可能节省的内存和处理器更多的内存。

Just think of your example - creating the String, doing the map look-up, garbage collecting the String, etc. 试想一下您的示例-创建String,进行地图查找,垃圾回收String等。

That's a lot more work than just creating a new Double. 这比创建一个新的Double还要多得多的工作。

The HashMap itself will use a lot of memory. HashMap本身将使用大量内存。

Considering the number of possible Double values are you going to expire them from the cache somehow or will it keep growing for ever? 考虑到可能的Double值的数量,您打算以某种方式使它们从缓存中过期还是会永远增长?

It is somewhat telling that the natural key to use into a cache of doubles would be a "new Double" itself (almost certainly faster than the String approach). 某种程度上讲,用于双精度缓存的自然键将是“新Double”本身(几乎可以肯定比String方法更快)。

You also need to consider equality. 您还需要考虑平等。 For example try in java: 例如,尝试使用java:

System.out.println((1-0.9)==0.1);

The result is false as the double values generated by each half are different by a miniscule fraction. 结果是错误的,因为每个半部生成的双精度值相差很小。 But if you get a Double from your cache would you want the same Double for (1-0.9) as for (0.1) ? 但是,如果从缓存中获取Double,那么您想要(1-0.9)和(0.1)拥有相同的Double吗?

So you are opening up a massive complicated can of worms, for incredibly tiny savings (in fact you are unlikely to save, most likely you will actually slow things down and use more memory not less). 因此,您正在打开大量复杂的蠕虫罐,以节省极少的费用(实际上,您不太可能进行保存,很可能实际上会放慢速度并使用更多的内存,而不是更少)。

The only real way to get much better efficiency is just by using double instead of Double (although I realize that's not always possible). 获得更高效率的唯一真实方法就是使用double而不是Double(尽管我意识到这并不总是可能的)。

Aside from very few values (1.0, 0.0 jump to mind) there isn't such a overwhelming clustering of occurance for small integer values like there is for Integer, so its very hard to come up with a cache that has a reasonable hit rate for common applications. 除了很少的值(请记住1.0、0.0)之外,对于像Integer这样的小整数值而言,还没有如此庞大的出现簇,因此很难提供具有合理命中率的缓存常见的应用程序。

Second, its not simple to cheaply detect if a Double represents to same value as another, there are edge cases with +0.0, -0.0 and NaN (possibly a few more I didn't think of). 其次,便宜地检测Double是否表示与另一个相同的值并不容易,存在+ 0.0,-0.0和NaN的边缘情况(可能还有我没想到的一些情况)。 Next problem there is no convenient mapping from cached values to a simple cache structure (int maps pretty naturally to Integer[]). 下一个问题是,从缓存的值到简单的缓存结构没有方便的映射(int很自然地映射到Integer [])。

Considering all these problems, there is probably simply no generally worthwhile implementation option for caching of Double values. 考虑到所有这些问题,可能根本就没有通用的缓存Double值的实现选项。 That doesn't mean you can't come up with one for specialized cases, but before spending time on that you may want to investigate alternatives that are easier to implement / yield potentially better returns (eg double instead of Double). 这并不意味着您无法针对特殊情况提出建议,但是在花时间之前,您可能需要研究更易于实施的替代方案/可能产生更好的回报(例如,用Double代替Double)。 If its just that one HashMap, I wouldn't even bother trying to optimize unless it proved to be the bottleneck of my application. 如果仅仅是一个HashMap,除非证明它是我的应用程序瓶颈,否则我什至都不会尝试进行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM