Home > Software engineering >  Which way of converting int[] to Integer[] is fastest?
Which way of converting int[] to Integer[] is fastest?

Time:05-10

I am trying to convert a very long int[] with length of 1,000,000 to Integer[] so that I can sort it with a custom comparator. I have done the following:

private static Integer[] convert(int[] intArray) {
        Integer[] ans = new Integer[arr.length];
        for (int i = 0; i < arr.length; i  ) {
            ans[i] = arr[i];
        }
        return ans;
    }

It works well for me but I have also come across

Integer[] ans = Arrays.stream(intArray).boxed().toArray( Integer[]::new );

and

Integer[] ans = IntStream.of(intArray).boxed().toArray( Integer[]::new );

Is there any of them that is significantly faster than the rest? Or is there any other approach that is fast enough to shorten the run-time?

CodePudding user response:

Is there any of them that is significantly faster than the rest?

You realize the question you're asking is akin to:

"I have 500,000 screws to screw into place. Unfortunately, I can't be bothered to go out and buy a screwdriver. I do have a clawhammer and an old shoe. Should I use the clawhammer to bash these things into place, or is the shoe a better option?"

The answer is clearly: Uh, neither. Go get a screwdriver, please.

To put it differently, if the 'cost' of converting to Integer[] first is 1000 points of cost in some arbitrary unit, then the difference in the options you listed is probably between 0.01 and 0.05 points - i.e. dwarfed so much, it's irrelevant. Thus, the direct answer to your question? It just does not matter.

You have 2 options:

  1. Performance is completely irrelevant. In which case this is fine, and there's absolutely no point to actually answering this question.

  2. You care about performance quite a bit. In which case, this Integer[] plan needs to be off the table.

Assuming you might be intrigued by option 2, you have various options.

The easiest one is to enjoy the extensive java ecosystem. Someone's been here before and made an excellent class just for this purpose. It abstracts the concept of an int array and gives you all sorts of useful methods, including sorting, and the team that made it is extremely concerned about performance, so they put in the many, many, many personweeks it takes to do proper performance analysis (between hotspot, pipelining CPUs, and today's complex OSes, it's much harder than you might think!).

Thus, I present you: IntArrayList. It has a .sortThis() method, as well as a .sortThis(IntComparator c) method, which you can use for sorting purposes.

There are a few others out there, searching the web for 'java primitive collections' should find them all, if for some reason the excellent eclipse collections project isn't to your liking (NB: You don't need eclipse-the-IDE to use it. It's a general purpose library that so happens to be maintained by the eclipse team).

If you must handroll it, searching the web for how to implement quicksort in java is not hard, thus, you can easily write your own 'sort this int array for me' code. Not that I would reinvent that particular wheel. Just pointing out that it's not too difficult if you must.

  • Related