简体   繁体   中英

How do you concurrently run 4 if statements in python?

does anyone know how to run two if statements at the same time in python?

for i in range(160):

    luca.speed = 1
    if luca.pos() > nick.pos() and luca.pos() > bob.pos():
        luca.backward(luca.speed + 2)
    if luca.pos() < nick.pos() and luca.pos() < bob.pos():
        luca.forward(luca.speed + 3)

    nick.speed = 1
    if luca.pos() > nick.pos() and bob.pos() > nick.pos():
        nick.forward(nick.speed + 8.5)
    if luca.pos() < nick.pos() and bob.pos() < nick.pos():
        nick.forward(nick.speed)

I am trying to make these 4 if statements run at the same time instead of one after the other.

What exactly do you mean with at the same time .

1.) Python (the default python implementation) uses the GIL (global interpreter lock), so you will never execute two python statements on multiple CPUs at the same time.

So performance wise there will be no benefit.

If you just mean, that the fact, the the first if statement might change the values from the if statement and thus render the second check false, then simple caching of values, that you use in your if expression will suffice.

for i in range(160):

    luca.speed = 1
    luca_pos = luca.pos()
    nic_pos = nic.pos()
    bob_pos = bob.pos()
    if luca_pos > nick_pos and luca_pos > bob_pos:
        luca.backward(luca.speed + 2)
    if luca_pos < nick_pos and luca_pos < bob_pos:
        luca.forward(luca.speed + 3)

    nick.speed = 1
    if luca_pos > nick_pos and bob_pos > nick_pos:
        nick.forward(nick.speed + 8.5)
    if luca_pos < nick_pos and bob_pos < nick_pos:
        nick.forward(nick.speed)

If I understand you correctly, you don't really want to run those statements in parallel (4 instructions at once), but rather you want single evaluation instead of 4 evals, right?

Right now you in both best case and worst case run 4 if, each with 2 comparisons, so 8 ops each time. Let's try to refactor the code - it needs the assumption that either luca.pos never equals nick.pos, or that we can replace > with >=:

luca.speed = 1
nick.speed = 1
if luca.pos() >= nick.pos():
  if luca.pos() > bob.pos():
    luca.backward(luca.speed + 2)
  if bob.pos() > nick.pos():
    nick.forward(nick.speed + 8.5)
else:
  if luca.pos() < bob.pos():
    luca.forward(luca.speed + 3)
  if bob.pos() < nick.pos():
    nick.forward(nick.speed)

This is 3 ops in both best and worst case.

This would be much easier (and clearer) if you separate the position determination from the movement actions using temporary variables:

for i in range(160):

    lucaAhead = luca.pos() > nick.pos() and luca.pos() > bob.pos()
    lucaBack  = luca.pos() < nick.pos() and luca.pos() < bob.pos()
    nickBack  = luca.pos() > nick.pos() and bob.pos() > nick.pos()
    nickAhead = luca.pos() < nick.pos() and bob.pos() < nick.pos()

    luca.speed = 1
    if lucaAhead:
        luca.backward(luca.speed + 2)
    if lucaBack:
        luca.forward(luca.speed + 3)

    nick.speed = 1
    if nickBack:
        nick.forward(nick.speed + 8.5)
    if nickAhead:
        nick.forward(nick.speed)

There also seems to be an inconsistency in the movement direction between luca and nick (luca moving backward when ahead but nick still going forward). There is not enough information in the question to determine if that is intended or accidental

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM