I'm doing metrics collection for a piece of code and want to store a collection of time differences (type primitive long
) for later analysis
The insert operation for this collection should be as efficient as possible to add least overhead to the results.
I first tested out a ConcurrentLinkedQueue<Long>
collection. This gave the worst performance (probably due to boxing/unboxing)
I've currently settled on using a synchronized gnu.trove.TLongArrayList
which is almost 7 times faster for a data set of 5 million longs.
Any recommendations for other collection libraries that may be good candidates to benchmark for this use case would be gratefully acknowledged. I took a look at the guava API, but couldn't seem t开发者_运维技巧o find anything
Something you could do to improve performance is to cut the size of the data type. If you can reduce it to an int
it would help. (often the difference between two calls to nanoTime() is less than 2 billion)
You can set a good starting size for the collection. esp if you know how many you are likely to have.
If you know the maximum number of values you will record you can use int[]
with a possible counter
if the maximum is not reached. This will me faster than using an Object.
There's a new version of Trove in the pipeline (the latest is 3.0.0-RC2). This page says that Trove 3 is 10% to 20% faster that Trove 2.
Unfortunately:
- Trove 3 has API compatibility breaking changes.
- The online javadocs are not available yet.
- You can't get it from Maven Central yet. (You can't even get Trove 2.1.0 ... tsk, tsk.)
You should try fastutil. Depends on the scenario, it is possible that fastutil is faster than trove4j
I'm not sure if your situation allows this, but did you consider saving your data in a separate, unsynchronized data structure for each thread? Something like a ThreadLocal containing a TLongArrayList. This would remove the synchronization overhead.
If you know ahead of time the size of the collection, you could use a single unsynchronized long[]
array combined with an AtomicInteger
counter to get the next insert position.
精彩评论