【发布时间】:2020-03-07 04:18:36
【问题描述】:
假设我有以下代码
def vc_count(word, low, high):
if low > high:
return 0, 0
v, c = vc_count(word, low+1, high)
if word[low] in "aeiouAEIOU":
return v+1, c
else:
return v, c+1
def vc_count(word, low, high):
if low > high:
return 0, 0
v, c = vc_count(word, low+1, high)
vowels = "aeiouAEIOU"
if word[low] in vowels:
return v+1, c
else:
return v, c+1
在第二个版本中创建了一个名为“元音”的字符串对象,而我在第一个版本中只写了“aeiouAEIOU”。
这两者之间会存在运行时差异或空间使用差异吗?
另外,第一个版本的调用堆栈中会显示一个临时变量吗?如果不是,python完成in操作后是否就丢弃它?
【问题讨论】:
-
速度差异应该是微不足道的。在第二个版本中,
vowels只是一个引用字符串的局部变量。
标签: python namespaces garbage-collection runtime temporary