简体   繁体   中英

Convert List<Integer> to int[] - Pure Java vs ArrayUtils benchmark issue

I recently had to optimise an api call in a high performance system that did some List<Integer> to int[] conversion. My initial finding was that for my expected average amount of elements pure java seemed A LOT faster than the currently used Apache Commons ArrayUtils. Factor 100-200 even and I was sort of amazed. Problem was I only ran my test on one pair of conversions at a time, restarting my little test program for each number of elements I wanted to test. Doing that, pure java was way faster for any number of elements up to several k's where it started to even out. Now I sat down to write a nicer test program that runs and outputs the results for a number of different sized lists in one go and my results are quite different.

It seems ArrayUtils is only slower on the first run, then faster on all subsequent runs regardless of list size, and regardless of number of element on the first run.

You can find my test class here: http://pastebin.com/9EYLZQKV

Please help me pick holes in it, as is now I don't get why I get the output I get.

Many thanks!

You need to account for loading times and warmup in your code. The built in classes may be called first and so it may already be loaded, and warmed up.

A loop is not optimised until it has been run at least 10,000 times. If you don't know when this is I suggest running the code for at least half a second to two seconds before starting your timing. I suggest repeating the test for at least 2 to 10 seconds and average the results.

BTW The fastest way to convert from List<Integer> to int[] is to avoid needing to convert in the first place. If you use TIntArrayList or something like it, you can use the same structure without copying or converting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM