小编alr*_*lta的帖子

Python 匹配大小写(开关)性能

我期望 Python match/case对每个案例有相同的时间访问权限,但似乎我错了。有什么好的解释为什么吗?

让我们使用以下示例:

def match_case(decimal):
    match decimal:
        case '0':
            return "000"
        case '1':
            return "001"
        case '2':
            return "010"
        case '3':
            return "011"
        case '4':
            return "100"
        case '5':
            return "101"
        case '6':
            return "110"
        case '7':
            return "111"
        case _:
            return "NA"
Run Code Online (Sandbox Code Playgroud)

并定义一个快速工具来测量时间:

import time
def measure_time(funcion):
    def measured_function(*args, **kwargs):
        init = time.time()
        c = funcion(*args, **kwargs)
        print(f"Input: {args[1]} Time: {time.time() - init}")
        return c
    return measured_function

@measure_time
def repeat(function, input):
    return [function(input) for i …
Run Code Online (Sandbox Code Playgroud)

python match switch-statement python-3.x

12
推荐指数
2
解决办法
9040
查看次数

标签 统计

match ×1

python ×1

python-3.x ×1

switch-statement ×1