JavaNewbie_M107
JavaNewbie_M107

Reputation: 2037

Should I prefer code duplication for better performance?

Let's say I have two overloaded methods, void foo(int arg) and void foo(int[] args). Both do the same processing on the integer parameters. Now, I've implemented them in this way

void foo(int arg){
    // Some processing.
}

void foo(int[] args){
    for(int i : args)
        //The same processing as above.
}

Now, I know it is a better design principle to avoid code duplication, so the second method can also be implemented as :

void foo(int[] args){
    for(int i : args)
        foo(i);
}

However, since I am calling the method inside several times, and since method calls add overheads, this approach would make it slower. So, my question is : Which approach should I use?

Upvotes: 2

Views: 367

Answers (3)

icyitscold
icyitscold

Reputation: 1151

I understand that this is just a representative example but you're talking about an extremely small overhead here. In general, I think it's standard practice to design for code maintainability first and for performance second. I'd even go as far as to say that performance optimization shouldn't be brought into consideration until there is a specific performance issue within your codebase (as indicated by a robust performance test suite) that you need to address.

If you're really that concerned about performance optimization, you wouldn't be writing in Java anyway....

Upvotes: 1

user207421
user207421

Reputation: 310957

Get rid of the first one altogether, and change the second one to:

void foo(int...args)

with the same code body. Then you won't have either repetition or an extra method call, just the loop-control overhead, which isn't going to kill you.

Upvotes: 1

Mohayemin
Mohayemin

Reputation: 3870

You are talking about a really insignificant overhead. I have a little program that will help you understand how insignificant it is:

class Bar {
    void foo(double d) {        
        double y = (d + 1) * d / 7.1 % 31.3 + 13.12 * 20.002;
    }

    void foo1(double[] args) {
        for (double d : args) {
            foo(d);
        }
    }

    void foo2(double[] args) {
        for (double d : args) {
            double y = (d + 1) * d / 7.1 % 31.3 + 13.12 * 20.002;
        }
    }
}

Here is a test and sample run

   public class Main {
        public static void test(int n) {
            System.out.print(n + ",");

            double is[] = new double[n];
            for (int i = 0; i < is.length; i++) {
                is[i] = i * 1.3;
            }

            Bar bar = new Bar();
            long span;

            long start = System.currentTimeMillis();
            bar.foo1(is);
            span = System.currentTimeMillis() - start;
            System.out.print(span + ",");

            start = System.currentTimeMillis();
            bar.foo2(is);
            span = System.currentTimeMillis() - start;
            System.out.print(span + "\n");
        }

        public static void main(String[] args) {
            test(10000000);
            test(20000000);
            test(30000000);
            test(40000000);
            test(50000000);
            test(60000000);
        }
    }

And here is the a output:

10000000,389,383
20000000,743,766
30000000,1130,1113
40000000,1497,1474
50000000,1866,1853
60000000,2243,2239

More complex the foo method is, more insignificant the difference will be. So IMHO forget about the performance, think about maintainability.

Upvotes: 11

Related Questions