0

i'm having a little trouble understanding how the CircularFifoQueue Class works. So for my requirements, i need a FIFO Queue of fixed Size (about 6000 Elements). At frist i was using the ArrayDequeue, but it was performing rather badly. Then i read about CircularFifoQueue and tried it out. I can see a boost in Performance, but it still isn't fast.

My Question is now: what happens if the Queue is full and i add an element? Is the whole underlying array copied? Is there some offset, that will be set, e.g.

  head = (head + 1) % size;

If the latter is the case, then i guess my algorithm is performing poorly.

Thanks!

hh32
  • 170
  • 10
  • have you tried and meassured it? As far as I can see from the [sources](https://commons.apache.org/proper/commons-collections/cobertura/org.apache.commons.collections4.queue.CircularFifoQueue.html): when adding a new element to a full list the remove() method is invoked, thus removing an element. This behaviour is also documented in the javadocs for add(). – bratkartoffel Jun 23 '15 at 15:14
  • @bratkartoffel yes, i did, the most time is spent performing the offer() method – hh32 Jun 23 '15 at 15:16

1 Answers1

3

The docs says the following about insertion in a CircularFifoQueue :

If the queue is full, the least recently added element is discarded so that a new element can be inserted.

When it comes to performance, it is to be noted that aside from the add, remove, peek, poll and offer methods which performs in constant time, all methods of this data structure perform in linear time, or worse.

Halim Qarroum
  • 13,985
  • 4
  • 46
  • 71
  • can you recommend a datastructure that performs better, when often adding elements to the fulll queue? – hh32 Jun 23 '15 at 15:25
  • I don't see particular issues with using a `CircularFifoQueue` then since insertion performs in constant time O(1) and what the method does if fairly simple (you can take a look at its implementation). So I wouldn't worry much about insertion performances. – Halim Qarroum Jun 23 '15 at 15:36
  • Can you rather post the code that is involved in inserting elements in your `CircularFifoQueue` ? – Halim Qarroum Jun 23 '15 at 15:39
  • i dont think that posting that code would be of any help. i'll try and explain. So basically i have an Event Based Sensor, sending me event packages (can be rather big, but at most 16384). these events are then processed by two filters (21x21). filtering also involves a temporal component, which is why i store the old ones. so in the worst case i perform 16384 x 21 x 21 x 2 = 14450688 Operations. but this upper bound is almost never reached. package size is usually around 1000 - 2000 elements – hh32 Jun 23 '15 at 15:42
  • I think that you should ask a new question in order for it to get more attention than the current one. You talk about the operations you perform in your program when processing events and it boils down to what an operation is and what it does involve computing and memory wise. It is quite difficult to understand what your implementation does given your description. I would try to avoid as much as I can to interact with the queue if possible given your temporal constraints. – Halim Qarroum Jun 23 '15 at 15:59