[ 
https://issues.apache.org/jira/browse/HIVE-5018?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13756731#comment-13756731
 ] 

Benjamin Jakobus commented on HIVE-5018:
----------------------------------------

Here is some sample code:
{quote}
        /**
         * Examine the performance difference between declaring variables 
inside loops
         * and declaring them outside of loops.
         */
        public class InLoopInstantiationTest {

    public InLoopInstantiationTest() {
        long start = System.currentTimeMillis();
        SessionIdentifierGenerator gen = new SessionIdentifierGenerator();
        for (int i = 0; i < 10000; i++) {
            FooBar f = new FooBar();
            Integer i1 = new Integer(i);
            String s = gen.nextSessionId();
        }
        long end = System.currentTimeMillis();
        System.out.println("in loop instantiation took " + (end - start) + " 
milliseconds");

        start = System.currentTimeMillis();
        FooBar f;
        Integer i1;
        String s;
        for (int i = 0; i < 10000; i++) {
            f = new FooBar();
            i1 = new Integer(i);
            s = gen.nextSessionId();
        }
        end = System.currentTimeMillis();
        System.out.println("avoiding in loop instantiation took " + (end - 
start) + " milliseconds");

    }

    public static void main(String[] args) {
        new InLoopInstantiationTest();
    }

    private class FooBar {

        private String foo = "asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd"
                + "asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd"
                + 
"asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd";
    }
    
    public final class SessionIdentifierGenerator {

        private SecureRandom random = new SecureRandom();

        public String nextSessionId() {
            return new BigInteger(130, random).toString(32);
        }
    }
}

{quote}

The arithmetic script that I used to test this code in Hive is:

{quote}
SELECT (dataset.age * dataset.gpa + 3) AS F1, (dataset.age/dataset.gpa - 1.5) 
AS F2
FROM    dataset
WHERE   dataset.gpa > 0;
{quote}


                
> Avoiding object instantiation in loops (issue 6)
> ------------------------------------------------
>
>                 Key: HIVE-5018
>                 URL: https://issues.apache.org/jira/browse/HIVE-5018
>             Project: Hive
>          Issue Type: Sub-task
>            Reporter: Benjamin Jakobus
>            Assignee: Benjamin Jakobus
>            Priority: Minor
>             Fix For: 0.12.0
>
>         Attachments: HIVE-5018.1.patch.txt
>
>
> Object instantiation inside loops is very expensive. Where possible, object 
> references should be created outside the loop so that they can be reused.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to