我有两个模块:moduleParent和moduleChild
我在moduleParent中做了这样的事情:
import moduleChild
#a bunch of code
start = time.time()
moduleChild.childFunction()
finish = time.time()
print "calling child function takes:", finish-start, "total seconds"
#a bunch of code
我在moduleChild中做了这样的事情:
^{pr2}$输出如下:
calling child function takes: .24 total seconds
child function says it takes: 0.0 total seconds
所以我的问题是,这些。额外的24秒从哪里来?在
谢谢你的专业知识。在
#这是“儿童游戏”的实际代码。真的不需要。24秒。在
def getResources(show, resourceName='', resourceType=''):
'''
get a list of resources with the given name
@show: show name
@resourceName: name of resource
@resourceType: type of resource
@return: list of resource dictionaries
'''
t1 = time.time()
cmd = r'C:\tester.exe -cmdFile "C:\%s\info.txt" -user root -pwd root'%show
cmd += " -cmd findResources -machineFormatted "
if resourceName:
cmd += '-name %s'%resourceName
if resourceType:
cmd += '_' + resourceType.replace(".", "_") + "_"
proc=subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = proc.stdout.read()
output = output.strip()
resourceData = output.split("\r\n")
resourceData = resourceData[1:]
resourceList = []
for data in resourceData:
resourceId, resourceName, resourceType = data.split("|")
rTyp = "_" + resourceType.replace(".", "_") + "_"
shot, assetName = resourceName.split(rTyp)
resourceName = assetName
path = '//projects/%s/scenes/%s/%s/%s'%(show, shot, resourceType.replace(".", "/"), assetName)
resourceDict = {'id':resourceId, 'name':resourceName, 'type':resourceType, 'path':path }
resourceList.append(resourceDict)
t2 = time.time()
print (" ", t2 - t2, "seconds")
return resourceList
编辑2:我刚刚注意到子函数中有一个错误,print语句中有t2-t2
忽略以下内容:
调用函数本身有开销(设置堆栈空间、保存局部变量、返回等)。结果表明,您的函数非常简单,设置函数调用比运行代码本身花费的时间要长。
编辑:另外,调用计时器以及印刷广告的开销。现在我想起来了,打电话给print可以解释很多。24秒。IO速度慢。在
你不能通过运行一次来度量函数的时间,尤其是运行时间很短的函数。有很多因素可能会影响计时,其中最重要的是您在系统上运行的其他进程。在
像这样的事情我可能至少跑几百次。查看timeit模块。在
相关问题 更多 >
编程相关推荐