【问题标题】:if match case is faster then why we use if else如果 match case 更快那么为什么我们使用 if else
【发布时间】:2023-01-14 21:58:10
【问题描述】:

我想知道 match case 是否比 if else 快那么为什么我们使用 if else?enter link description here

【问题讨论】:

  • match不是一定更快。它不是像 C switch 语句那样的“计算转到”。它的重点是模式匹配。

标签: python


【解决方案1】:

If-else 语句可用于根据条件做出决定。 if-match case 比 if-else 快,但只能在条件已知且固定的情况下使用,例如:

如果 x == 5: print("x 是 5")

在这种情况下,条件已知且固定,因此 if-match case 是合适的选择。但是,如果条件未知并且可能会发生变化,则可以使用 if-else 语句,例如:

如果 x < 5: print("x 小于 5") 别的: print("x大于等于5")

在这种情况下,条件未知并且可能会发生变化,因此 if-else 是合适的选择。

【讨论】:

    猜你喜欢
    • 2017-10-25
    • 1970-01-01
    • 2010-12-20
    • 2019-12-29
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多