Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

performace to sort large Date objects ?

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a large java.util.Date objects with OR without duplication, which way has best performance to sort ?

Thank.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will assume you mean a large number of Date objects (because all java.util.Date Objects should be roughly the same size).

I suspect the best way to do your sorting will be to use Collections.sort(List<Date>). You would have to prove that that method was too slow or doesn't sort the way you want before you look for other options.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But another option would be to use a collection that orders objects as they are inserted. Since you allow doubles of the same Date the best bet would be a PriorityQueue<Date>. This pushes the cost of sorting forward to the addition of objects, but means there is little/no cost to reading the Dates in order.

When you use an un-order collection, or one that uses insertion order like List, you then use Collections.sort(). It means there is little cost to adding new objects, but before you read them you need to sort which can cost you time.

So if you need a continuous adds/reads you might find it easier and more efficient to use a PriorityQueue. If you collect a large number of Dates then after collecting them need to read them in order then Collections.sort might be better.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Relevant Links:
Collections
PriorityQueue
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic