Jordan Trainor
Jordan Trainor

Reputation: 2438

Set<> = Sets.newHashSet()

Ok so i reformated the post to make it a little easier to understand (sorry about all the pastebins but stack overflow was being dumb with code formatting)

Please note that I do not intend to have the ridiculous amount of data stored as I state below. The main reason I use the amount I said is to squeeze out as much efficiency as possible.

Lets say I have the following code(s)

the method that will be adding to the DropItemQueue (starts at floodFill(with depth 0) the other paramaters do not matter)

http://pastebin.com/3wqEb5cM

this is the same class and it will then call the dropItem method in Utils

http://pastebin.com/yEUW9Wad

My Utils.dropItem method is as follows

http://pastebin.com/c0eaWeMA

This is the ServerTickHandler.addDropItemQueue method and its variable storage

http://pastebin.com/Q4p5a4ja

Here is the DropItemQueue class

http://pastebin.com/wxCj9imN

If I was to say add 100000000 elements to this hashset I have noticed that it takes around 2 seconds to

iterate over everything in the hashset using This iterator is called every 1/20th of a seccond

http://pastebin.com/zSWg1kKu

2 seconds per iteration doesn't seem like much but with that amount of elements to get rid of every single element stored in the hash set it would take around 50 days

every time an element is parsed to the hashset the maxTicks is 1 more than the previous added element so basically every 1 second an item is dropped but due to the 2 seconds to iterate over everything its actually taking 3 seconds to drop an item which would make it take around 150 days to complete the iteration and flush every element out and complete

my question is would it be quicker to have multiple hash sets with less maximum elements lets say 1000 elements.

yes this would give me 100000 hashsets but due to them each being smaller would the iterations times be slower (all be it a very small increase in efficiency) or is there something better I can use other than a hashsets or are hashsets the best thing to use?

do note that if I did use multiple iterations of smaller iterations I could not use threads due to cross thread data redundancy

Upvotes: 0

Views: 1612

Answers (1)

Malcolm O'Hare
Malcolm O'Hare

Reputation: 4999

The HashSet data structure is designed for really only one purpose, which is to answer the question "Does this set contain this item". For any other use it not necessarily the most efficient.

For your use, it seems like a Queue would be a better choice.

Upvotes: 1

Related Questions