Can we make a faster Boole implementation?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I just learned about Mr. Wizard's very efficient code here.



list=RandomChoice[True,False,10^6];
AbsoluteTiming[l2=Developer`ToPackedArray@With[True=1,False=0,Evaluate@list];]
(*0.066533,Null*)


Compare that to Boole.



AbsoluteTiming[l3=Boole[list];]
l2===l3
(*0.262770,Null*)
(* True *)


It seems we can make a faster Boole. I tried to use the code above to do that, but it doesn't work.



boole[list_]:=Developer`ToPackedArray@With[True=1,False=0,Evaluate@list];
list=True,True,False,True,False;
boole[list]
(*True,True,False,True,False*)


Why doesn't boole above work, and how can it be fixed?










share|improve this question



























    up vote
    2
    down vote

    favorite












    I just learned about Mr. Wizard's very efficient code here.



    list=RandomChoice[True,False,10^6];
    AbsoluteTiming[l2=Developer`ToPackedArray@With[True=1,False=0,Evaluate@list];]
    (*0.066533,Null*)


    Compare that to Boole.



    AbsoluteTiming[l3=Boole[list];]
    l2===l3
    (*0.262770,Null*)
    (* True *)


    It seems we can make a faster Boole. I tried to use the code above to do that, but it doesn't work.



    boole[list_]:=Developer`ToPackedArray@With[True=1,False=0,Evaluate@list];
    list=True,True,False,True,False;
    boole[list]
    (*True,True,False,True,False*)


    Why doesn't boole above work, and how can it be fixed?










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I just learned about Mr. Wizard's very efficient code here.



      list=RandomChoice[True,False,10^6];
      AbsoluteTiming[l2=Developer`ToPackedArray@With[True=1,False=0,Evaluate@list];]
      (*0.066533,Null*)


      Compare that to Boole.



      AbsoluteTiming[l3=Boole[list];]
      l2===l3
      (*0.262770,Null*)
      (* True *)


      It seems we can make a faster Boole. I tried to use the code above to do that, but it doesn't work.



      boole[list_]:=Developer`ToPackedArray@With[True=1,False=0,Evaluate@list];
      list=True,True,False,True,False;
      boole[list]
      (*True,True,False,True,False*)


      Why doesn't boole above work, and how can it be fixed?










      share|improve this question















      I just learned about Mr. Wizard's very efficient code here.



      list=RandomChoice[True,False,10^6];
      AbsoluteTiming[l2=Developer`ToPackedArray@With[True=1,False=0,Evaluate@list];]
      (*0.066533,Null*)


      Compare that to Boole.



      AbsoluteTiming[l3=Boole[list];]
      l2===l3
      (*0.262770,Null*)
      (* True *)


      It seems we can make a faster Boole. I tried to use the code above to do that, but it doesn't work.



      boole[list_]:=Developer`ToPackedArray@With[True=1,False=0,Evaluate@list];
      list=True,True,False,True,False;
      boole[list]
      (*True,True,False,True,False*)


      Why doesn't boole above work, and how can it be fixed?







      performance-tuning function-construction core-language






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      Chris K

      5,99221739




      5,99221739










      asked 2 hours ago









      Ted Ersek

      2,8441230




      2,8441230




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          The problem is the automatic module variable renaming that happens with With. You can use TracePrint to see this:



          TracePrint[boole[list], _With]



          With[True$=1,False$=0,True,True,False,True,False]



          True, True, False, True, False




          One idea to circumvent this renaming is to use a pure function:



          Clear[boole]
          boole = Function[Developer`ToPackedArray @ With[True = 1, False = 0, #]];


          Then:



          boole[list]



          1, 1, 0, 1, 0







          share|improve this answer




















          • Thank you. Wizard and Carl are both WL experts. I am using version 10. Is the With[True=1,False-0 ,...] trick faster than Boole in version 11.3 ?
            – Ted Ersek
            1 hour ago


















          up vote
          1
          down vote













          See Using With to scope over pure functions, then try:



          boole[list_] := With @@ Hold[True = 1, False = 0, list] // Developer`ToPackedArray





          share|improve this answer




















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "387"
            ;
            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: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184765%2fcan-we-make-a-faster-boole-implementation%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            The problem is the automatic module variable renaming that happens with With. You can use TracePrint to see this:



            TracePrint[boole[list], _With]



            With[True$=1,False$=0,True,True,False,True,False]



            True, True, False, True, False




            One idea to circumvent this renaming is to use a pure function:



            Clear[boole]
            boole = Function[Developer`ToPackedArray @ With[True = 1, False = 0, #]];


            Then:



            boole[list]



            1, 1, 0, 1, 0







            share|improve this answer




















            • Thank you. Wizard and Carl are both WL experts. I am using version 10. Is the With[True=1,False-0 ,...] trick faster than Boole in version 11.3 ?
              – Ted Ersek
              1 hour ago















            up vote
            1
            down vote













            The problem is the automatic module variable renaming that happens with With. You can use TracePrint to see this:



            TracePrint[boole[list], _With]



            With[True$=1,False$=0,True,True,False,True,False]



            True, True, False, True, False




            One idea to circumvent this renaming is to use a pure function:



            Clear[boole]
            boole = Function[Developer`ToPackedArray @ With[True = 1, False = 0, #]];


            Then:



            boole[list]



            1, 1, 0, 1, 0







            share|improve this answer




















            • Thank you. Wizard and Carl are both WL experts. I am using version 10. Is the With[True=1,False-0 ,...] trick faster than Boole in version 11.3 ?
              – Ted Ersek
              1 hour ago













            up vote
            1
            down vote










            up vote
            1
            down vote









            The problem is the automatic module variable renaming that happens with With. You can use TracePrint to see this:



            TracePrint[boole[list], _With]



            With[True$=1,False$=0,True,True,False,True,False]



            True, True, False, True, False




            One idea to circumvent this renaming is to use a pure function:



            Clear[boole]
            boole = Function[Developer`ToPackedArray @ With[True = 1, False = 0, #]];


            Then:



            boole[list]



            1, 1, 0, 1, 0







            share|improve this answer












            The problem is the automatic module variable renaming that happens with With. You can use TracePrint to see this:



            TracePrint[boole[list], _With]



            With[True$=1,False$=0,True,True,False,True,False]



            True, True, False, True, False




            One idea to circumvent this renaming is to use a pure function:



            Clear[boole]
            boole = Function[Developer`ToPackedArray @ With[True = 1, False = 0, #]];


            Then:



            boole[list]



            1, 1, 0, 1, 0








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 hours ago









            Carl Woll

            62.7k281161




            62.7k281161











            • Thank you. Wizard and Carl are both WL experts. I am using version 10. Is the With[True=1,False-0 ,...] trick faster than Boole in version 11.3 ?
              – Ted Ersek
              1 hour ago

















            • Thank you. Wizard and Carl are both WL experts. I am using version 10. Is the With[True=1,False-0 ,...] trick faster than Boole in version 11.3 ?
              – Ted Ersek
              1 hour ago
















            Thank you. Wizard and Carl are both WL experts. I am using version 10. Is the With[True=1,False-0 ,...] trick faster than Boole in version 11.3 ?
            – Ted Ersek
            1 hour ago





            Thank you. Wizard and Carl are both WL experts. I am using version 10. Is the With[True=1,False-0 ,...] trick faster than Boole in version 11.3 ?
            – Ted Ersek
            1 hour ago











            up vote
            1
            down vote













            See Using With to scope over pure functions, then try:



            boole[list_] := With @@ Hold[True = 1, False = 0, list] // Developer`ToPackedArray





            share|improve this answer
























              up vote
              1
              down vote













              See Using With to scope over pure functions, then try:



              boole[list_] := With @@ Hold[True = 1, False = 0, list] // Developer`ToPackedArray





              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                See Using With to scope over pure functions, then try:



                boole[list_] := With @@ Hold[True = 1, False = 0, list] // Developer`ToPackedArray





                share|improve this answer












                See Using With to scope over pure functions, then try:



                boole[list_] := With @@ Hold[True = 1, False = 0, list] // Developer`ToPackedArray






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 10 mins ago









                Mr.Wizard♦

                228k294671023




                228k294671023



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184765%2fcan-we-make-a-faster-boole-implementation%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke