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 to 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
intit 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 possiblecounterif the maximum is not reached. This will me faster than using an Object.