存储2个数字中较大数字的最佳方式



我正在努力使我的python程序尽可能优化,但有一部分我不确定。我需要存储到目前为止获得的最大值,我有两个候选代码。哪一个是时间优化最多的?或者有更快的方法吗?

代码1:

if value > biggest_value:
biggest_value = value

代码2:

biggest_value = max(biggest_value, value)

上下文:

def check_palindrome(num):
num = str(num)
if len(num) < 1:
return True
else:
if num[0] == num[-1]:
return check_palindrome(num[1:-1])
else:
return False

def main():
biggest_product = 0
for a in range(100, 1000):
for b in range(100, 1000):
product = a * b
if check_palindrome(product):
# store the biggest_product here
return biggest_product
main()

代码2是更好的选择,因为在代码1中,您必须将数字与2个命令(<=(进行比较,而在代码2中,您只有一个命令。

祝你的程序好运,玩得开心!

乔纳斯

最新更新