在Java中实现固定大小的非阻塞队列的最有效方法是什么?

时间:2021-11-25 21:46:47

I am trying to find (or write) a Java class that represents a fixed-size, non-blocking, auto-discarding FIFO queue. (e.g. if the queue has a capacity of 100, putting item 101 removes item 1 then successfully appends item 101.) The answer to this question seems helpful, but I have an extra constraint - I need it to be fast, for capacities of around 100-1000.

我试图找到(或写入)代表固定大小,非阻塞,自动丢弃FIFO队列的Java类。 (例如,如果队列的容量为100,则放置项目101会移除项目1,然后成功附加项目101.)这个问题的答案看起来很有帮助,但我有一个额外的限制 - 我需要它快速,因为周围的容量100-1000。

The items in my queue are only Floats, so is it generally more efficient to use something like the AutoDiscardingDeque<Float> described in the linked question, or just to use a float[] and some System.arraycopy() manipulation to handle it?

我的队列中的项目只是浮点数,因此使用链接问题中描述的AutoDiscardingDeque 等通常更有效,或者只是使用float []和一些System.arraycopy()操作来处理它?

Alternatively, is there an even better solution that I haven't thought of?

或者,有没有更好的解决方案,我没有想到?

3 个解决方案

#1


2  

If you only ever need to use floats, then yes, a float[] will be optimal in the implementation. You shouldn't need to copy the array at all - just maintain a "start position" and an "end position". You already know the capacity, so you can create the array to start with and never budge from it.

如果你只需要使用浮点数,那么是的,float []将是实现中的最佳选择。您根本不需要复制数组 - 只需保持“开始位置”和“结束位置”。您已经知道了容量,因此您可以创建数组以从中开始并且永远不会让它变为现实。

Note that I'm not suggesting you use a float[] instead of a queue here - just that you can implement the queue using a float[]. Of course, that means you can't easily make it implement Deque<Float> which you may want it to, without incurring boxing/unboxing costs... but if you're happy only ever using the concrete class within your client code, you'll end up with efficiency savings.

请注意,我并不是建议你在这里使用float []而不是队列 - 只是你可以使用float []来实现队列。当然,这意味着你不能轻易地实现你可能想要它的Deque ,而不会产生装箱/拆箱费用......但如果你只是在客户代码中使用具体类,那么你最终会节省成本。

#2


1  

If you think you are going to want to perform a number of math related functions on your structure, specifically statistics functions like mean, max, min, ect., then you could use DescriptiveStatistics from Apache Commons Math (http://commons.apache.org/math/userguide/stat.html#a1.2_Descriptive_statistics). You can set your window size and it will automatically maintain your elements. However it takes doubles, not floats, so it might not be the perfect solution for you.

如果您认为您希望在结构上执行许多与数学相关的函数,特别是统计函数,如mean,max,min等,那么您可以使用Apache Commons Math中的DescriptiveStatistics(http://commons.apache) .ORG /数学/ userguide / stat.html#a1.2_Descriptive_statistics)。您可以设置窗口大小,它将自动维护您的元素。然而,它需要双打,而不是浮动,所以它可能不是你的完美解决方案。

#3


0  

I need it to be fast, for capacities of around 100-1000

我需要它快速,容量大约100-1000

Please, specify, which operations you need to be fast? Implementation is very sensible to how you are going to use it. If you need accessing it by index very often, than the solution above seems good enough

请指定您需要快速进行哪些操作?实施对于如何使用它非常明智。如果您需要经常通过索引访问它,那么上面的解决方案似乎已经足够了

#1


2  

If you only ever need to use floats, then yes, a float[] will be optimal in the implementation. You shouldn't need to copy the array at all - just maintain a "start position" and an "end position". You already know the capacity, so you can create the array to start with and never budge from it.

如果你只需要使用浮点数,那么是的,float []将是实现中的最佳选择。您根本不需要复制数组 - 只需保持“开始位置”和“结束位置”。您已经知道了容量,因此您可以创建数组以从中开始并且永远不会让它变为现实。

Note that I'm not suggesting you use a float[] instead of a queue here - just that you can implement the queue using a float[]. Of course, that means you can't easily make it implement Deque<Float> which you may want it to, without incurring boxing/unboxing costs... but if you're happy only ever using the concrete class within your client code, you'll end up with efficiency savings.

请注意,我并不是建议你在这里使用float []而不是队列 - 只是你可以使用float []来实现队列。当然,这意味着你不能轻易地实现你可能想要它的Deque ,而不会产生装箱/拆箱费用......但如果你只是在客户代码中使用具体类,那么你最终会节省成本。

#2


1  

If you think you are going to want to perform a number of math related functions on your structure, specifically statistics functions like mean, max, min, ect., then you could use DescriptiveStatistics from Apache Commons Math (http://commons.apache.org/math/userguide/stat.html#a1.2_Descriptive_statistics). You can set your window size and it will automatically maintain your elements. However it takes doubles, not floats, so it might not be the perfect solution for you.

如果您认为您希望在结构上执行许多与数学相关的函数,特别是统计函数,如mean,max,min等,那么您可以使用Apache Commons Math中的DescriptiveStatistics(http://commons.apache) .ORG /数学/ userguide / stat.html#a1.2_Descriptive_statistics)。您可以设置窗口大小,它将自动维护您的元素。然而,它需要双打,而不是浮动,所以它可能不是你的完美解决方案。

#3


0  

I need it to be fast, for capacities of around 100-1000

我需要它快速,容量大约100-1000

Please, specify, which operations you need to be fast? Implementation is very sensible to how you are going to use it. If you need accessing it by index very often, than the solution above seems good enough

请指定您需要快速进行哪些操作?实施对于如何使用它非常明智。如果您需要经常通过索引访问它,那么上面的解决方案似乎已经足够了