Performance of OR EQUAL vs EQUAL assignment in a for-loop












0















This question is mostly directed at compiled programming languages. It is purely out of curiosity because I believe the gain of performance in using one of the two operators would be so very small.



Consider a for-loop where when you meet a certain condition, you want to store true in a boolean:



b = false
for i in 1..N:
if someCondition(i):
b = true
moreThatNeedsToBeDone(i)
endfor


Now consider the same for loop with OR EQUAL instead



b = false
for i in 1..N:
if someCondition(i):
b |= true
moreThatNeedsToBeDone(i)
endfor


If the condition is met more than once, would the latter be faster in theory? Or atleast, would it do fewer operations? In general, the OR EQUAL evaluates the variable and if it is true, then it doesn't do anything hence there is no extra assignment compared to the EQUAL operator where it would STORE true multiple times. But writing this, I realize the OR EQUAL adds an extra operation anyways in order to evaluate/read the current value of the variable. So which would be faster or have fewer operations to do?










share|improve this question

























  • Depends on the ... following the assignment. You can also break the loop once b is set to true is ... is a no-op. The rest is marginal optimization that is so rarely relevant these days.

    – TT.
    Nov 25 '18 at 16:47











  • Yeah I thought about breaking once b is true but that's why I added the .... The ... is something that needs to be done, in other words we can't break before the loop is over. I know it's really not relevant, just a question that popped in my head and wondered if anyone could provide an answer on SO.

    – Fabrice Dugas
    Nov 25 '18 at 23:15


















0















This question is mostly directed at compiled programming languages. It is purely out of curiosity because I believe the gain of performance in using one of the two operators would be so very small.



Consider a for-loop where when you meet a certain condition, you want to store true in a boolean:



b = false
for i in 1..N:
if someCondition(i):
b = true
moreThatNeedsToBeDone(i)
endfor


Now consider the same for loop with OR EQUAL instead



b = false
for i in 1..N:
if someCondition(i):
b |= true
moreThatNeedsToBeDone(i)
endfor


If the condition is met more than once, would the latter be faster in theory? Or atleast, would it do fewer operations? In general, the OR EQUAL evaluates the variable and if it is true, then it doesn't do anything hence there is no extra assignment compared to the EQUAL operator where it would STORE true multiple times. But writing this, I realize the OR EQUAL adds an extra operation anyways in order to evaluate/read the current value of the variable. So which would be faster or have fewer operations to do?










share|improve this question

























  • Depends on the ... following the assignment. You can also break the loop once b is set to true is ... is a no-op. The rest is marginal optimization that is so rarely relevant these days.

    – TT.
    Nov 25 '18 at 16:47











  • Yeah I thought about breaking once b is true but that's why I added the .... The ... is something that needs to be done, in other words we can't break before the loop is over. I know it's really not relevant, just a question that popped in my head and wondered if anyone could provide an answer on SO.

    – Fabrice Dugas
    Nov 25 '18 at 23:15
















0












0








0








This question is mostly directed at compiled programming languages. It is purely out of curiosity because I believe the gain of performance in using one of the two operators would be so very small.



Consider a for-loop where when you meet a certain condition, you want to store true in a boolean:



b = false
for i in 1..N:
if someCondition(i):
b = true
moreThatNeedsToBeDone(i)
endfor


Now consider the same for loop with OR EQUAL instead



b = false
for i in 1..N:
if someCondition(i):
b |= true
moreThatNeedsToBeDone(i)
endfor


If the condition is met more than once, would the latter be faster in theory? Or atleast, would it do fewer operations? In general, the OR EQUAL evaluates the variable and if it is true, then it doesn't do anything hence there is no extra assignment compared to the EQUAL operator where it would STORE true multiple times. But writing this, I realize the OR EQUAL adds an extra operation anyways in order to evaluate/read the current value of the variable. So which would be faster or have fewer operations to do?










share|improve this question
















This question is mostly directed at compiled programming languages. It is purely out of curiosity because I believe the gain of performance in using one of the two operators would be so very small.



Consider a for-loop where when you meet a certain condition, you want to store true in a boolean:



b = false
for i in 1..N:
if someCondition(i):
b = true
moreThatNeedsToBeDone(i)
endfor


Now consider the same for loop with OR EQUAL instead



b = false
for i in 1..N:
if someCondition(i):
b |= true
moreThatNeedsToBeDone(i)
endfor


If the condition is met more than once, would the latter be faster in theory? Or atleast, would it do fewer operations? In general, the OR EQUAL evaluates the variable and if it is true, then it doesn't do anything hence there is no extra assignment compared to the EQUAL operator where it would STORE true multiple times. But writing this, I realize the OR EQUAL adds an extra operation anyways in order to evaluate/read the current value of the variable. So which would be faster or have fewer operations to do?







performance variable-assignment assignment-operator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 23:18







Fabrice Dugas

















asked Nov 25 '18 at 15:59









Fabrice DugasFabrice Dugas

149110




149110













  • Depends on the ... following the assignment. You can also break the loop once b is set to true is ... is a no-op. The rest is marginal optimization that is so rarely relevant these days.

    – TT.
    Nov 25 '18 at 16:47











  • Yeah I thought about breaking once b is true but that's why I added the .... The ... is something that needs to be done, in other words we can't break before the loop is over. I know it's really not relevant, just a question that popped in my head and wondered if anyone could provide an answer on SO.

    – Fabrice Dugas
    Nov 25 '18 at 23:15





















  • Depends on the ... following the assignment. You can also break the loop once b is set to true is ... is a no-op. The rest is marginal optimization that is so rarely relevant these days.

    – TT.
    Nov 25 '18 at 16:47











  • Yeah I thought about breaking once b is true but that's why I added the .... The ... is something that needs to be done, in other words we can't break before the loop is over. I know it's really not relevant, just a question that popped in my head and wondered if anyone could provide an answer on SO.

    – Fabrice Dugas
    Nov 25 '18 at 23:15



















Depends on the ... following the assignment. You can also break the loop once b is set to true is ... is a no-op. The rest is marginal optimization that is so rarely relevant these days.

– TT.
Nov 25 '18 at 16:47





Depends on the ... following the assignment. You can also break the loop once b is set to true is ... is a no-op. The rest is marginal optimization that is so rarely relevant these days.

– TT.
Nov 25 '18 at 16:47













Yeah I thought about breaking once b is true but that's why I added the .... The ... is something that needs to be done, in other words we can't break before the loop is over. I know it's really not relevant, just a question that popped in my head and wondered if anyone could provide an answer on SO.

– Fabrice Dugas
Nov 25 '18 at 23:15







Yeah I thought about breaking once b is true but that's why I added the .... The ... is something that needs to be done, in other words we can't break before the loop is over. I know it's really not relevant, just a question that popped in my head and wondered if anyone could provide an answer on SO.

– Fabrice Dugas
Nov 25 '18 at 23:15














1 Answer
1






active

oldest

votes


















1














Most current compilers optimize to the best run time, meaning the resulting assembler or machine code would probably be identical. You could out of interest take a peek at the assembly generated for the two versions. I bet they are identical.



If the compiler would translate literally, the |= operation will not involve evaluating the variable to know if OR'ing the value should be done or not. The compiler will simply emit an or instruction as that would be equivalent and faster than first checking the variable (which could end up clearing the instruction pipeline).



Without optimization, the assembly generated could be (ax is a processor register):



or ax,1  ; for b|=true

mov ax,1 ; for b=true


I doubt on current processors this makes any difference in execution speed at all. Even if it would make a difference, we're talking about such a marginal optimization versus load times from memory to cache to processor/registers, or versus thread switches/process switches on CPU's, or versus branch misprediction in pipelined microprocessors and such.






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',
    autoActivateHeartbeat: false,
    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%2f53469245%2fperformance-of-or-equal-vs-equal-assignment-in-a-for-loop%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









    1














    Most current compilers optimize to the best run time, meaning the resulting assembler or machine code would probably be identical. You could out of interest take a peek at the assembly generated for the two versions. I bet they are identical.



    If the compiler would translate literally, the |= operation will not involve evaluating the variable to know if OR'ing the value should be done or not. The compiler will simply emit an or instruction as that would be equivalent and faster than first checking the variable (which could end up clearing the instruction pipeline).



    Without optimization, the assembly generated could be (ax is a processor register):



    or ax,1  ; for b|=true

    mov ax,1 ; for b=true


    I doubt on current processors this makes any difference in execution speed at all. Even if it would make a difference, we're talking about such a marginal optimization versus load times from memory to cache to processor/registers, or versus thread switches/process switches on CPU's, or versus branch misprediction in pipelined microprocessors and such.






    share|improve this answer




























      1














      Most current compilers optimize to the best run time, meaning the resulting assembler or machine code would probably be identical. You could out of interest take a peek at the assembly generated for the two versions. I bet they are identical.



      If the compiler would translate literally, the |= operation will not involve evaluating the variable to know if OR'ing the value should be done or not. The compiler will simply emit an or instruction as that would be equivalent and faster than first checking the variable (which could end up clearing the instruction pipeline).



      Without optimization, the assembly generated could be (ax is a processor register):



      or ax,1  ; for b|=true

      mov ax,1 ; for b=true


      I doubt on current processors this makes any difference in execution speed at all. Even if it would make a difference, we're talking about such a marginal optimization versus load times from memory to cache to processor/registers, or versus thread switches/process switches on CPU's, or versus branch misprediction in pipelined microprocessors and such.






      share|improve this answer


























        1












        1








        1







        Most current compilers optimize to the best run time, meaning the resulting assembler or machine code would probably be identical. You could out of interest take a peek at the assembly generated for the two versions. I bet they are identical.



        If the compiler would translate literally, the |= operation will not involve evaluating the variable to know if OR'ing the value should be done or not. The compiler will simply emit an or instruction as that would be equivalent and faster than first checking the variable (which could end up clearing the instruction pipeline).



        Without optimization, the assembly generated could be (ax is a processor register):



        or ax,1  ; for b|=true

        mov ax,1 ; for b=true


        I doubt on current processors this makes any difference in execution speed at all. Even if it would make a difference, we're talking about such a marginal optimization versus load times from memory to cache to processor/registers, or versus thread switches/process switches on CPU's, or versus branch misprediction in pipelined microprocessors and such.






        share|improve this answer













        Most current compilers optimize to the best run time, meaning the resulting assembler or machine code would probably be identical. You could out of interest take a peek at the assembly generated for the two versions. I bet they are identical.



        If the compiler would translate literally, the |= operation will not involve evaluating the variable to know if OR'ing the value should be done or not. The compiler will simply emit an or instruction as that would be equivalent and faster than first checking the variable (which could end up clearing the instruction pipeline).



        Without optimization, the assembly generated could be (ax is a processor register):



        or ax,1  ; for b|=true

        mov ax,1 ; for b=true


        I doubt on current processors this makes any difference in execution speed at all. Even if it would make a difference, we're talking about such a marginal optimization versus load times from memory to cache to processor/registers, or versus thread switches/process switches on CPU's, or versus branch misprediction in pipelined microprocessors and such.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 3:47









        TT.TT.

        12.5k63464




        12.5k63464
































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469245%2fperformance-of-or-equal-vs-equal-assignment-in-a-for-loop%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

            Wiesbaden

            Marschland

            Dieringhausen