简体   繁体   English

你如何在python中同时运行4个if语句?

[英]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?有谁知道如何在python中同时运行两个if语句?

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.我试图让这 4 个 if 语句同时运行,而不是一个接一个地运行。

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. 1.) Python(默认的python实现)使用GIL(全局解释器锁),所以你永远不会在多个CPU上同时执行两个python语句。

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.如果您只是想说,第一个 if 语句可能会更改 if 语句中的值,从而使第二个检查为 false,那么您在 if 表达式中使用的值的简单缓存就足够了。

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?如果我理解正确,您并不是真的想并行运行这些语句(一次 4 条指令),而是需要单次评估而不是 4 次评估,对吗?

Right now you in both best case and worst case run 4 if, each with 2 comparisons, so 8 ops each time.现在你在最好的情况和最坏的情况下运行 4 if,每个都有 2 个比较,所以每次 8 个操作。 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.pos 永远不等于 nick.pos,或者我们可以用 >= 替换 >:

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.这是最好和最坏情况下的 3 个操作。

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问题中没有足够的信息来确定这是有意的还是偶然的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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