0

I know very little of bytecode and compiled code. I have a method that is in a path where performance is very critical. There is an alternative way of doing the same with java's API and an apache library. But I'm concerned that involving two extra classes will render the method slower. So my question is, regarding performance, is there any advantage in using your own code?

These are the method with my own code and using libraries:

public final static List<Long> longArrayToLongList(long[] array)
{
    if (array == null)
    {
        return null;
    }

    List<Long> list = new ArrayList<Long>(array.length);
    for (long value : array)
    {
        list.add(Long.valueOf(value));
    }
    return list;
}

public final static List<Long> longArrayToLongListWithLib(long[] array)
{
    return Arrays.asList(ArrayUtils.toObject(array));
}
user3748908
  • 885
  • 2
  • 9
  • 26
  • 2
    It is very much doubtful that your array conversion will be the bootleneck of your application... – meskobalazs Dec 15 '14 at 11:50
  • 3
    With regards to performance, the first rule is: **measure**, don't guess. Measure if the code you are looking at is really causing a performance problem, and only optimize it if it really is. Your intuition (trying to guess by looking at the code) is almost always incorrect. – Jesper Dec 15 '14 at 11:52
  • possible duplicate of [How do I write a correct micro-benchmark in Java?](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Joe Dec 15 '14 at 11:53
  • And some comment on the Apache Commons libraries: they are more about utility, than performance. – meskobalazs Dec 15 '14 at 11:56
  • @Joe not really; from what I understand the OP asks whether executing code from third party libs would be any different from executing code you write yourself -- hence my answer – fge Dec 15 '14 at 11:57
  • @Joe How is that a duplicate? I wanted an answer like fge's, with some hints about what happens with compiled code. Not tips about writing my own benchmarks. – user3748908 Dec 15 '14 at 12:13
  • possible duplicate of [Java - calling static methods vs manual inlining - performance overhead](http://stackoverflow.com/questions/4970015/java-calling-static-methods-vs-manual-inlining-performance-overhead) – default locale Dec 15 '14 at 12:23
  • @defaultlocale nope... The question here is "my own code vs other's code"; it is about "class equality" with regards to the JVM. Not the JIT. – fge Dec 15 '14 at 12:24
  • @fge your answer's great, +1. I was searching for a correct duplicate and this was the best I've managed to find. Anyway, isn't "class equality" somewhat obvious? I doubt that OP thinks that her/his classes are naturally superior to Apache's. The question is about the impact of method calls and it was already covered. – default locale Dec 15 '14 at 12:34
  • 1
    @defaultlocale well, not _that_ obvious apparently ;) Understandable when you have no knowledge of the internals... You sometimes ask yourself some questions which can appear silly to people who _do_ know something about them. – fge Dec 15 '14 at 12:45
  • @defaultlocale "I was searching for a correct duplicate and this was the best I've managed to find." Seriously? The best you've managed to find? Did it occur to you that it might not be a duplicate? That if you don't find any obvious duplicate you don't need to go with "the best you manage to find"? – user3748908 Dec 15 '14 at 14:05
  • @user3748908 Good points! I still think that your question was covered before and I hope that answers in the question I linked to can help you with your question. Anyway, I'm glad that you've solved your problem. Good luck! – default locale Dec 15 '14 at 17:49

2 Answers2

6

regarding performance, is there any advantage in using your own code?

At the JVM level, none.

A class, whether it be yours or a library's, is loaded by a classloader, and after that all classes are equal. Methods are invoke{special,static,virtual,interface}d [1] the same way, the methods' bytecode is optimized using exactly the same JIT mechanisms (inlining, loop unrolling, escape analysis, you name it) etc.

The only difference therefore lies in the performance of the code itself. And for this, you want to do microbenchmarking (using, for instance, jmh or caliper).

[1] those are the four major bytecode execution instructions, along with invokedynamic

fge
  • 119,121
  • 33
  • 254
  • 329
  • But in this case, when using libraries the classloader will have to load (at least) two extra classes, Arrays and ArrayUtils. Doesn't that have any extra cost? – user3748908 Dec 15 '14 at 12:16
  • Well yes, but compared to all the classes in the JRE, it is negligible ;) – fge Dec 15 '14 at 12:18
0

Invocation time for a method depends on the JVM.

But if you are talking about, method performance, it purely depends on the implementation.

Let's say if there are two method that does the same thing, but different implementation. Their performance might vary on the same JVM. Obviously better implementation run's faster than the other.

Once an object, either your own or from a library, is in the memory JVM treats them both equally.

You will have to run and test them manually for your use case to derive on a conclusion on which one to use.

Since a library method, may do more things that you actually need, you'd get better performance if you use a stripped down version of it.

WarFox
  • 4,933
  • 3
  • 28
  • 32