If method argument is a primitive int, then myArrayList.contains(primitiveArg) within a loop within a method...











up vote
0
down vote

favorite












I have code like this:



public int getDistanceToNumber(int number) {
List<Integer> tuple5 = null;
int distanceCounter = 0;
for (int i = 0; i < allDraws.size(); i++) {
tuple5 = allDraws.get(i).getTupleAsList();
if (tuple5.contains(number)) { // autoboxing primitive ?

}

}

return 0;
}


The question is - shall I make method argument Integer like int getDistanceToNumber(Integer number) for autoboxing from primitive into Integer to happen only once, or there is no performance issue.



This piece of code inside loop runs over 100K times...










share|improve this question






















  • Did you look at the generated bytecode and/or perform benchmarks?
    – UnholySheep
    Nov 11 at 19:35










  • Have you benchmarked this with JMH and determined that it's in need of optimization?
    – Jacob G.
    Nov 11 at 19:35






  • 2




    Don't change the method signature. Instead, you can create a local variable with the boxed int value. As for performance, the sequential search of the List is more of a problem. However, as others have suggested, don't micro-optimize the code without first analyzing the code by profiling it.
    – Andreas
    Nov 11 at 19:36

















up vote
0
down vote

favorite












I have code like this:



public int getDistanceToNumber(int number) {
List<Integer> tuple5 = null;
int distanceCounter = 0;
for (int i = 0; i < allDraws.size(); i++) {
tuple5 = allDraws.get(i).getTupleAsList();
if (tuple5.contains(number)) { // autoboxing primitive ?

}

}

return 0;
}


The question is - shall I make method argument Integer like int getDistanceToNumber(Integer number) for autoboxing from primitive into Integer to happen only once, or there is no performance issue.



This piece of code inside loop runs over 100K times...










share|improve this question






















  • Did you look at the generated bytecode and/or perform benchmarks?
    – UnholySheep
    Nov 11 at 19:35










  • Have you benchmarked this with JMH and determined that it's in need of optimization?
    – Jacob G.
    Nov 11 at 19:35






  • 2




    Don't change the method signature. Instead, you can create a local variable with the boxed int value. As for performance, the sequential search of the List is more of a problem. However, as others have suggested, don't micro-optimize the code without first analyzing the code by profiling it.
    – Andreas
    Nov 11 at 19:36















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have code like this:



public int getDistanceToNumber(int number) {
List<Integer> tuple5 = null;
int distanceCounter = 0;
for (int i = 0; i < allDraws.size(); i++) {
tuple5 = allDraws.get(i).getTupleAsList();
if (tuple5.contains(number)) { // autoboxing primitive ?

}

}

return 0;
}


The question is - shall I make method argument Integer like int getDistanceToNumber(Integer number) for autoboxing from primitive into Integer to happen only once, or there is no performance issue.



This piece of code inside loop runs over 100K times...










share|improve this question













I have code like this:



public int getDistanceToNumber(int number) {
List<Integer> tuple5 = null;
int distanceCounter = 0;
for (int i = 0; i < allDraws.size(); i++) {
tuple5 = allDraws.get(i).getTupleAsList();
if (tuple5.contains(number)) { // autoboxing primitive ?

}

}

return 0;
}


The question is - shall I make method argument Integer like int getDistanceToNumber(Integer number) for autoboxing from primitive into Integer to happen only once, or there is no performance issue.



This piece of code inside loop runs over 100K times...







java performance autoboxing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 19:33









caasdads

1327




1327












  • Did you look at the generated bytecode and/or perform benchmarks?
    – UnholySheep
    Nov 11 at 19:35










  • Have you benchmarked this with JMH and determined that it's in need of optimization?
    – Jacob G.
    Nov 11 at 19:35






  • 2




    Don't change the method signature. Instead, you can create a local variable with the boxed int value. As for performance, the sequential search of the List is more of a problem. However, as others have suggested, don't micro-optimize the code without first analyzing the code by profiling it.
    – Andreas
    Nov 11 at 19:36




















  • Did you look at the generated bytecode and/or perform benchmarks?
    – UnholySheep
    Nov 11 at 19:35










  • Have you benchmarked this with JMH and determined that it's in need of optimization?
    – Jacob G.
    Nov 11 at 19:35






  • 2




    Don't change the method signature. Instead, you can create a local variable with the boxed int value. As for performance, the sequential search of the List is more of a problem. However, as others have suggested, don't micro-optimize the code without first analyzing the code by profiling it.
    – Andreas
    Nov 11 at 19:36


















Did you look at the generated bytecode and/or perform benchmarks?
– UnholySheep
Nov 11 at 19:35




Did you look at the generated bytecode and/or perform benchmarks?
– UnholySheep
Nov 11 at 19:35












Have you benchmarked this with JMH and determined that it's in need of optimization?
– Jacob G.
Nov 11 at 19:35




Have you benchmarked this with JMH and determined that it's in need of optimization?
– Jacob G.
Nov 11 at 19:35




2




2




Don't change the method signature. Instead, you can create a local variable with the boxed int value. As for performance, the sequential search of the List is more of a problem. However, as others have suggested, don't micro-optimize the code without first analyzing the code by profiling it.
– Andreas
Nov 11 at 19:36






Don't change the method signature. Instead, you can create a local variable with the boxed int value. As for performance, the sequential search of the List is more of a problem. However, as others have suggested, don't micro-optimize the code without first analyzing the code by profiling it.
– Andreas
Nov 11 at 19:36














1 Answer
1






active

oldest

votes

















up vote
0
down vote













You should test that under a JMH.




  • You can avoid the boxing problem by using a Integer.valueOf(int) once and passing it to List::contains(Object).

  • The compiler may be efficient enough to understand that number is never changed and do that for you.


For the rest, without more information (type of allDraws ?), there might be other optimization to do before boxing conversion.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252422%2fif-method-argument-is-a-primitive-int-then-myarraylist-containsprimitivearg-w%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You should test that under a JMH.




    • You can avoid the boxing problem by using a Integer.valueOf(int) once and passing it to List::contains(Object).

    • The compiler may be efficient enough to understand that number is never changed and do that for you.


    For the rest, without more information (type of allDraws ?), there might be other optimization to do before boxing conversion.






    share|improve this answer

























      up vote
      0
      down vote













      You should test that under a JMH.




      • You can avoid the boxing problem by using a Integer.valueOf(int) once and passing it to List::contains(Object).

      • The compiler may be efficient enough to understand that number is never changed and do that for you.


      For the rest, without more information (type of allDraws ?), there might be other optimization to do before boxing conversion.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You should test that under a JMH.




        • You can avoid the boxing problem by using a Integer.valueOf(int) once and passing it to List::contains(Object).

        • The compiler may be efficient enough to understand that number is never changed and do that for you.


        For the rest, without more information (type of allDraws ?), there might be other optimization to do before boxing conversion.






        share|improve this answer












        You should test that under a JMH.




        • You can avoid the boxing problem by using a Integer.valueOf(int) once and passing it to List::contains(Object).

        • The compiler may be efficient enough to understand that number is never changed and do that for you.


        For the rest, without more information (type of allDraws ?), there might be other optimization to do before boxing conversion.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 19:39









        NoDataFound

        5,5621740




        5,5621740






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252422%2fif-method-argument-is-a-primitive-int-then-myarraylist-containsprimitivearg-w%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma