1

Please confirm which of the two codes are efficient a or b . Where string is defines inside loop or outside loop. Kindly sugggest.

a)

for(int i=0;i<configIndexNumber.size();i++){
                     ConfigurationDetailsObject oldIndex =(ConfigurationDetailsObject)configIndexNumber.get(i);
                     String Key = oldIndex.getVehConfigNo()+oldIndex.getBaseEngineKey()+oldIndex.getEngineCode()+oldIndex.getTestGroupId()+oldIndex.getTransConfig()+oldIndex.getInertiaWeightClassNo()+oldIndex.getAxleRatioValue();
                        if(!configIdxNnb.containsKey(Key))
                        {
                                newConfigIndexList.add(oldIndex);
                                configIdxNnb.put(Key,oldIndex);
                        }

                 }

b)

String Key=null;

 for(int i=0;i<configIndexNumber.size();i++){
                     ConfigurationDetailsObject oldIndex =(ConfigurationDetailsObject)configIndexNumber.get(i);
                     Key = oldIndex.getVehConfigNo()+oldIndex.getBaseEngineKey()+oldIndex.getEngineCode()+oldIndex.getTestGroupId()+oldIndex.getTransConfig()+oldIndex.getInertiaWeightClassNo()+oldIndex.getAxleRatioValue();
                        if(!configIdxNnb.containsKey(Key))
                        {
                                newConfigIndexList.add(oldIndex);
                                configIdxNnb.put(Key,oldIndex);
                        }

                 }
1
  • I would focus on making your code more readable (and following Java naming conventions) before you worry about performance - and please put more effort into formatting questions in future. (I'd also use a separate class for the key, rather than just using string concatenation.) Commented Dec 27, 2013 at 11:13

5 Answers 5

1

There's no difference between the two cases. In both cases you are assigning a new value to the String variable.

I know what optimization you have in mind. That kind of optimization is routinely done by compilers these days. But again, it doesn't apply in your case anyway.

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

Comments

0

It doesn't matter in terms of efficiency, but (a) is the better of the two - it is better for code readability to define variables in the smallest possible scope.

Comments

0

Unlike C++ ,Local variables need not be initialised separately in java prior to the scope in which it is being used. So there is no need for initialising

String Key = null 

prior to its usage. It is a better programming practice to initialise a local variable only when it is used . It is also efficient in a certain sense that you allocate memory for the variable in the heap only when it is being put to use inside the for loop.

Refer the oracle docs for greater understanding on variable initialisations

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

Comments

0

There will be a minor difference in memory aspect. JVM is enough intelligent to manage.

But you should declare a variable in the smallest scope required . i.e the first case is looking good to me.

I did'nt remember the exact quote from efffective java, It goes something like

The Garbage collector do the best when we use smallest possible scope.

But if you want to access that variable outside the loop the second is the option.

Comments

0

There is very little/ negligible difference b/w the execution times of the 2... The compiler optimizes your code...

public class Test
{
    public static void main(String[] args) {

        long l = System.nanoTime();
        func1("hi");
        System.out.println(System.nanoTime()-l);
        long l1 = System.nanoTime();
        func2("hi");
        System.out.println(System.nanoTime()-l1);
    }

    public static void func1(String str)
    {
        for(int i=0;i<10000;i++)
        {
            String s= str ;
        //  System.out.println(s);
        }
    }

    public static void func2(String str)
    {String s= null;
        for(int i=0;i<10000;i++)
        {
             s= str ;
        //  System.out.println(s);
        }
    }
}

O/p : 
1. 277530
   265557

2. 267694
  243747 

Comments

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.