Multiple equates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jzakiya

    Multiple equates

    I looked online and in books, but couldn't find a definitive answer to
    this.

    I have an array and set multiple elements to either True or False at
    one time.

    Question: Which way is faster (or does it matter)?

    1)

    array[x1]=array[x2]=........= array[x10] = \
    array[x11]=array[x12]=... = array[x20] = \
    .......
    .......
    array[x40]=array[x41]=....= array[x50] = False (or True)


    2)

    array[x1]=array[x2]=........= array[x10] = False
    array[x11]=array[x12]=... = array[x20] = False
    .......
    .......
    array[x40]=array[x41]=....= array[x50] = False

  • Arnaud Delobelle

    #2
    Re: Multiple equates

    jzakiya <jzakiya@mail.c omwrites:
    I looked online and in books, but couldn't find a definitive answer to
    this.
    >
    I have an array and set multiple elements to either True or False at
    one time.
    >
    Question: Which way is faster (or does it matter)?
    >
    1)
    >
    array[x1]=array[x2]=........= array[x10] = \
    array[x11]=array[x12]=... = array[x20] = \
    ......
    ......
    array[x40]=array[x41]=....= array[x50] = False (or True)
    >
    >
    2)
    >
    array[x1]=array[x2]=........= array[x10] = False
    array[x11]=array[x12]=... = array[x20] = False
    ......
    ......
    array[x40]=array[x41]=....= array[x50] = False
    It doesn't matter as none of this is valid Python. In Python you have to
    write

    array[x1] = False
    array[x2] = False

    Etc...

    --
    Arnaud

    Comment

    • jzakiya

      #3
      Re: Multiple equates

      On Nov 17, 2:10 pm, Arnaud Delobelle <arno...@google mail.comwrote:
      jzakiya <jzak...@mail.c omwrites:
      I looked online and in books, but couldn't find a definitive answer to
      this.
      >
      I have an array and set multiple elements to either True or False at
      one time.
      >
      Question: Which way is faster (or does it matter)?
      >
      1)
      >
      array[x1]=array[x2]=........= array[x10] = \
      array[x11]=array[x12]=... = array[x20] = \
      ......
      ......
      array[x40]=array[x41]=....= array[x50] = False (or True)
      >
      2)
      >
      array[x1]=array[x2]=........= array[x10] = False
      array[x11]=array[x12]=... = array[x20] = False
      ......
      ......
      array[x40]=array[x41]=....= array[x50] = False
      >
      It doesn't matter as none of this is valid Python. In Python you have to
      write
      >
      array[x1] = False
      array[x2] = False
      >
      Etc...
      >
      --
      Arnaud
      Could you explain please.
      Python allows this, so is it just not considered good idiomatic
      python?

      jz

      Comment

      • Diez B. Roggisch

        #4
        Re: Multiple equates

        Arnaud Delobelle wrote:
        jzakiya <jzakiya@mail.c omwrites:
        >
        >I looked online and in books, but couldn't find a definitive answer to
        >this.
        >>
        >I have an array and set multiple elements to either True or False at
        >one time.
        >>
        >Question: Which way is faster (or does it matter)?
        >>
        >1)
        >>
        >array[x1]=array[x2]=........= array[x10] = \
        >array[x11]=array[x12]=... = array[x20] = \
        >......
        >......
        >array[x40]=array[x41]=....= array[x50] = False (or True)
        >>
        >>
        >2)
        >>
        >array[x1]=array[x2]=........= array[x10] = False
        >array[x11]=array[x12]=... = array[x20] = False
        >......
        >......
        >array[x40]=array[x41]=....= array[x50] = False
        >
        It doesn't matter as none of this is valid Python. In Python you have to
        write
        >
        array[x1] = False
        array[x2] = False
        >
        Etc...
        >
        No.


        l = range(10)
        l[0] = l[1] = 100

        And there seems to be a small difference between both versions,
        but I can't judge for sure if that favors one of them - yet I tend to think
        it's not worth the effort...

        import dis

        def a():
        l = range(10)
        l[0] = l[1] = 100

        def b():
        l = range(10)
        l[0] = 100
        l[1] = 100

        print "------------ a ---------------"
        dis.dis(a)
        print "------------ b ---------------"
        dis.dis(b)

        ------------ a ---------------
        4 0 LOAD_GLOBAL 0 (range)
        3 LOAD_CONST 1 (10)
        6 CALL_FUNCTION 1
        9 STORE_FAST 0 (l)

        5 12 LOAD_CONST 2 (100)
        * 15 DUP_TOP
        16 LOAD_FAST 0 (l)
        19 LOAD_CONST 3 (0)
        22 STORE_SUBSCR
        23 LOAD_FAST 0 (l)
        26 LOAD_CONST 4 (1)
        29 STORE_SUBSCR
        30 LOAD_CONST 0 (None)
        33 RETURN_VALUE
        ------------ b ---------------
        8 0 LOAD_GLOBAL 0 (range)
        3 LOAD_CONST 1 (10)
        6 CALL_FUNCTION 1
        9 STORE_FAST 0 (l)

        9 12 LOAD_CONST 2 (100)
        15 LOAD_FAST 0 (l)
        18 LOAD_CONST 3 (0)
        21 STORE_SUBSCR

        10 22 LOAD_CONST 2 (100)
        25 LOAD_FAST 0 (l)
        28 LOAD_CONST 4 (1)
        31 STORE_SUBSCR
        32 LOAD_CONST 0 (None)
        35 RETURN_VALUE






        Comment

        • Tim Chase

          #5
          Re: Multiple equates

          It doesn't matter as none of this is valid Python. In Python you have to
          write
          >
          array[x1] = False
          array[x2] = False
          Uh...not so much...
          >>a = [1,2,3,4,5]
          >>x1, x2 = 1, 3
          >>a[x1] = a[x2] = False
          >>a
          [1, False, 3, False, 5]

          Works for me.

          To the OP, I think rather than cluttering my code, I'd just
          create a loop

          for i in [x1,x2,x3,x4,... x1024]:
          a[i] = False

          From Diez's disassembly of it (as an aside, nifty little intro
          to dis.dis()...tha nks, Diez!), it looks like it boils down to "is
          DUP_TOP faster than LOAD_CONST" because the rest of the
          operations. At this point, it's pretty nitty-gritty.

          Unless the code is in an inner loop somewhere, the simple loop
          should be more than fast enough. Without knowing the source of
          the [x1,...] index variables, it's hard to tell if there's a more
          optimal way to do this.

          -tkc



          Comment

          • Arnaud Delobelle

            #6
            Re: Multiple equates

            Arnaud Delobelle <arnodel@google mail.comwrites:
            jzakiya <jzakiya@mail.c omwrites:
            >
            >I looked online and in books, but couldn't find a definitive answer to
            >this.
            >>
            >I have an array and set multiple elements to either True or False at
            >one time.
            >>
            >Question: Which way is faster (or does it matter)?
            >>
            >1)
            >>
            >array[x1]=array[x2]=........= array[x10] = \
            >array[x11]=array[x12]=... = array[x20] = \
            >......
            >......
            >array[x40]=array[x41]=....= array[x50] = False (or True)
            >>
            >>
            >2)
            >>
            >array[x1]=array[x2]=........= array[x10] = False
            >array[x11]=array[x12]=... = array[x20] = False
            >......
            >......
            >array[x40]=array[x41]=....= array[x50] = False
            >
            It doesn't matter as none of this is valid Python. In Python you have to
            write
            >
            array[x1] = False
            array[x2] = False
            >
            Etc...
            Sorry, I don't know what came over me then, some kind of brain
            short-circuit... No doubt plenty of people will correct me before this
            is posted!

            Of course it's valid Python, but it's not very stylish. Assuming your
            array is a list, you could put all your indices that are meant to be
            True in a list:

            indices = [x1, x2, x3, ..., x50]

            Then loop over them:

            for i in indices:
            array[i] = True

            It might not be as fast of course

            --
            Arnaud

            Comment

            • Cameron Laird

              #7
              Re: Multiple equates

              In article <mailman.4155.1 226950874.3487. python-list@python.org >,
              Tim Chase <python.list@ti m.thechases.com wrote:

              Comment

              • CarlFK

                #8
                Re: Multiple equates

                On Nov 17, 2:54 pm, cla...@lairds.u s (Cameron Laird) wrote:
                In article <mailman.4155.1 226950874.3487. python-l...@python.org >,
                Tim Chase  <python.l...@ti m.thechases.com wrote:
                                        .
                                        .
                                        .>To the OP, I think rather than cluttering my code, I'd just
                create a loop
                >
                  for i in [x1,x2,x3,x4,... x1024]:
                    a[i] = False
                >
                                        .
                                        .
                                        .
                I much prefer this, too.
                >
                I've been trying to decide if there's any sober reason to advocate
                the one-liner
                >
                    map(lambda i: a.__setitem__(i , False), [x1, x2, x3, ..., x1024])
                >
                My answer:  no.
                well, the OP was "...faster? " not "best, cleanest, pythonic..." so
                the reason would be speed.

                (looks faster, don't know for sure)
                (and yeah, looks ugly. but we need the speed...)

                Carl K

                Comment

                • Tim Roberts

                  #9
                  Re: Multiple equates

                  jzakiya <jzakiya@mail.c omwrote:
                  >
                  >I looked online and in books, but couldn't find a definitive answer to
                  >this.
                  >
                  >I have an array and set multiple elements to either True or False at
                  >one time.
                  >
                  >Question: Which way is faster (or does it matter)?
                  Answer: it does not matter. This is premature optimization. First, make
                  it work. Then, figure out whether it is fast enough. THEN, figure out
                  what's taking the most time.

                  I'd be very, very surprised if this was an important part of your run time.
                  --
                  Tim Roberts, timr@probo.com
                  Providenza & Boekelheide, Inc.

                  Comment

                  Working...