1

I was using these in my code but I think they are may be not as fast as manual coded procedure. I had searched the and found some articles which say that System.arraycopy() is actually faster than copying an array manually. I am not quite sure whether that is correct or not.

Also, the function Array.sort() is the fast compared to what we write in code?

// I am merging the arrays here into a new integer array called newarray3 
    int[] newarray3= new int[input1.length + input2.length];
    System.arraycopy(input1, 0, newarray3, 0, input1.length);
    System.arraycopy(input2, 0, newarray3, input1.length, input2.length);

    //sorting the array.
    Arrays.sort(newarray3);

input1 and input2 are two arrays which are to be merged and then sorted. I want to know whether coding this way is making my program slower. Or could it be something else. Please help.

5
  • What is manula process? Commented Apr 7, 2013 at 14:18
  • 2
    Have you tried profiling this code yourself? Commented Apr 7, 2013 at 14:18
  • typing mistake I corrected it. I meant "manual" meaning we write the code our-self Commented Apr 7, 2013 at 14:19
  • @Bernard yes I tried to the best of my knowledge and I can't think of anything more, which is why I need some guide to help me out. Commented Apr 7, 2013 at 14:20
  • 1
    System.arrayCopy() is way faster and moreover it is implemented in native code. Check this page. Commented Apr 7, 2013 at 14:23

2 Answers 2

2

System.arraycopy will be faster that what you can do by hand in virtually all circumstances, since it can do "bulk" data moves, vs an element at a time. The main exception would be relatively small arrays, since the initial processing inside arraycopy, selecting which algorithm to use, is non-trivial.

Re sort, there's no single sort algorithm that is optimal in all conditions.

Sign up to request clarification or add additional context in comments.

Comments

1

arrayCopy() is a native method, so yes it might be faster than a pure Java implementation coded by hand. On the other hand, sort() is a pure java method, but designed for generic sorting - and the specific algorithm used depends on the data type of the array, take a look at this post to understand the details.

You could make your own sorting implementation that's faster by improving the comparison between objects and specializing the algorithm for a certain data type, in fact this is the approach recommended in the book Java Performance Tuning. Anyway, you won't know for sure until a profiler is used for comparison.

2 Comments

So it could mean that the reason for my code to have a little bit more time complexity is due to the sort()? what sorting algorithm does the sort() use, can you tell ?
It's possible (profile it first!), but bear in mind that that algorithm is heavily optimized already. Look at this post to understand the algorithm being used

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.