views:

121

answers:

3

I'm new to Java programming.
I am curious about speed of execution and also speed of creation and distruction of objects.
I've got several methods like the following:

private static void getAbsoluteThrottleB() {
    int A = Integer.parseInt(Status.LineToken.nextToken());
    Status.AbsoluteThrottleB=A*100/255;
    Log.level1("Absolute Throttle Position B: " + Status.AbsoluteThrottleB);
}

and

   private static void getWBO2S8Volts() {
        int A = Integer.parseInt(Status.LineToken.nextToken());
        int B = Integer.parseInt(Status.LineToken.nextToken());
        int C = Integer.parseInt(Status.LineToken.nextToken());
        int D = Integer.parseInt(Status.LineToken.nextToken());
        Status.WBO2S8Volts=((A*256)+B)/32768;
        Status.WBO2S8VoltsEquivalenceRatio=((C*256)+D)/256 - 128;

        Log.level1("WideBand Sensor 8 Voltage: " + Double.toString(Status.WBO2S8Volts));
        Log.level1("WideBand Sensor 8 Volt EQR:" + Double.toString(Status.WBO2S8VoltsEquivalenceRatio));

Would it be wise to create a separate method to process the data since it is repetative? Or would it just be faster to execute it as a single method? I have several of these which would need to be rewritten and I am wondering if it would actually improve speed of execution or if it is just as good, or if there is a number of instructions where it becomes a good idea to create a new method.

Basically, what is faster or when does it become faster to use a single method to process objects versus using another method to process several like objects?

It seems like at runtime, pulling a new variable, then performing a math operation on it is quicker then creating a new method and then pulling a varible then performing a math operation on it. My question is really where the speed is at..

These methods are all called only to read data and set a Status.Variable. There are nearly 200 methods in my class which generate data.

+4  A: 

The speed difference of invoking a piece of code inside a method or outside of it is negligible. Specially compared with using the right algorithm for the task.

I would recommend you to use the method anyway, not for performance but for maintainability. If you need to change one line of code which turn out to introduce a bug or something and you have this code segment copy/pasted in 50 different places, it would be much harder to change ( and spot ) than having it in one single place.

So, don't worry about the performance penalty introduced by using methods because, it is practically nothing( even better, the VM may inline some of the calls )

OscarRyz
Ok. I understand. It would appear to me that the Java community frowns upon optimization.
Adam Outler
:) That's what it looks like, hehe, but not really, is ***premature*** what concerns ( us ). Once you have a working solution, that does what it should, and have no or very little bugs in your app, you can grab a profiler and know what is a bottle neck in your code, but just until you have a working solution.
OscarRyz
+1  A: 

I am curious about speed of execution and also speed of creation and destruction of objects.

Creation of objects in Java is fast enough that you shouldn't need to worry about it, except in extreme and unusual situations.

Destruction of objects in a modern Java implementation has zero cost ... unless you use finalizers. And there are very few situations that you should even think of using a finalizer.

Basically, what is faster or when does it become faster to use a single method to process objects versus using another method to process several like objects?

The difference is negligible relative to everything else that is going on.


As @S.Lott says: "Please don't micro-optimize". Focus on writing code that is simple, clear, precise and correct, and that uses the most appropriate algorithms. Only "micro" optimize when you have clear evidence of a critical bottleneck.

Stephen C
+1  A: 

I think S. Lott's comment on your question probably hits the nail perfectly on the head - there's no point optimizing code until you're sure the code in question actually needs it. You'll most likely end up spending a lot of time and effort for next to no gain, otherwise.

I'll also second Support's answer, in that the difference in execution time between invoking a separate method and invoking the code inline is negligible (this was actually what I wanted to post, but he kinda beat me to it). It may even be zero, if an optimizing compiler or JIT decides to inline the method anyway (I'm not sure if there are any such compilers/JITs for Java, however).

There is one advantage of the separate method approach however - if you separate your data-processing code into a separate method, you could in theory achieve some increased performance by having that method called from a separate thread, thus decoupling your (possibly time-consuming) processing code from your other code.

Mac