mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-04 02:42:49 -04:00
optimized imports in profile code
This commit is contained in:
parent
86d00a7024
commit
66d112fbe5
@ -834,9 +834,20 @@ def getProfileResultString():
|
|||||||
global _ProfileResultStr
|
global _ProfileResultStr
|
||||||
return _ProfileResultStr
|
return _ProfileResultStr
|
||||||
|
|
||||||
|
class PModules:
|
||||||
|
# holder for modules used by profiling code, avoids import every time code is called
|
||||||
|
builtin = None
|
||||||
|
profile = None
|
||||||
|
os = None
|
||||||
|
pstats = None
|
||||||
|
|
||||||
def profile(callback, name, terse, log=True):
|
def profile(callback, name, terse, log=True):
|
||||||
global _ProfileResultStr
|
global _ProfileResultStr
|
||||||
import __builtin__
|
if PModules.builtin is None:
|
||||||
|
import __builtin__
|
||||||
|
PModules.builtin = __builtin__
|
||||||
|
else:
|
||||||
|
__builtin__ = PModules.builtin
|
||||||
if 'globalProfileFunc' in __builtin__.__dict__:
|
if 'globalProfileFunc' in __builtin__.__dict__:
|
||||||
# rats. Python profiler is not re-entrant...
|
# rats. Python profiler is not re-entrant...
|
||||||
base.notify.warning(
|
base.notify.warning(
|
||||||
@ -926,14 +937,22 @@ def startProfile(filename=PyUtilProfileDefaultFilename,
|
|||||||
callInfo=1,
|
callInfo=1,
|
||||||
cmd='run()'):
|
cmd='run()'):
|
||||||
# uniquify the filename to allow multiple processes to profile simultaneously
|
# uniquify the filename to allow multiple processes to profile simultaneously
|
||||||
filename = '%s.%s' % (filename, randUint31())
|
filename = '%s.%s.%s' % (filename, randUint31(), randUint31())
|
||||||
import profile
|
if PModules.profile is None:
|
||||||
|
import profile
|
||||||
|
PModules.profile = profile
|
||||||
|
else:
|
||||||
|
profile = PModules.profile
|
||||||
profile.run(cmd, filename)
|
profile.run(cmd, filename)
|
||||||
if silent:
|
if silent:
|
||||||
extractProfile(filename, lines, sorts, callInfo)
|
extractProfile(filename, lines, sorts, callInfo)
|
||||||
else:
|
else:
|
||||||
printProfile(filename, lines, sorts, callInfo)
|
printProfile(filename, lines, sorts, callInfo)
|
||||||
import os
|
if PModules.os is None:
|
||||||
|
import os
|
||||||
|
PModules.os = os
|
||||||
|
else:
|
||||||
|
os = PModules.os
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
|
||||||
# call these to see the results again, as a string or in the log
|
# call these to see the results again, as a string or in the log
|
||||||
@ -941,7 +960,11 @@ def printProfile(filename=PyUtilProfileDefaultFilename,
|
|||||||
lines=PyUtilProfileDefaultLines,
|
lines=PyUtilProfileDefaultLines,
|
||||||
sorts=PyUtilProfileDefaultSorts,
|
sorts=PyUtilProfileDefaultSorts,
|
||||||
callInfo=1):
|
callInfo=1):
|
||||||
import pstats
|
if PModules.pstats is None:
|
||||||
|
import pstats
|
||||||
|
PModules.pstats = pstats
|
||||||
|
else:
|
||||||
|
pstats = PModules.pstats
|
||||||
s = pstats.Stats(filename)
|
s = pstats.Stats(filename)
|
||||||
s.strip_dirs()
|
s.strip_dirs()
|
||||||
for sort in sorts:
|
for sort in sorts:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user