Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6ae7a7d1
Commit
6ae7a7d1
authored
Mar 30, 2009
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add simple tests for __import__ for future optimizations to importlib.
parent
ded31c47
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
0 deletions
+82
-0
Lib/importlib/test/benchmark.py
Lib/importlib/test/benchmark.py
+82
-0
No files found.
Lib/importlib/test/benchmark.py
0 → 100644
View file @
6ae7a7d1
from
.
import
util
from
.source
import
util
as
source_util
import
gc
import
decimal
import
imp
import
importlib
import
sys
import
timeit
def
bench_cache
(
import_
,
repeat
,
number
):
"""Measure the time it takes to pull from sys.modules."""
name
=
'<benchmark import>'
with
util
.
uncache
(
name
):
module
=
imp
.
new_module
(
name
)
sys
.
modules
[
name
]
=
module
runs
=
[]
for
x
in
range
(
repeat
):
start_time
=
timeit
.
default_timer
()
for
y
in
range
(
number
):
import_
(
name
)
end_time
=
timeit
.
default_timer
()
runs
.
append
(
end_time
-
start_time
)
return
min
(
runs
)
def
bench_importing_source
(
import_
,
repeat
,
number
,
loc
=
100000
):
"""Measure importing source from disk.
For worst-case scenario, the line endings are
\
\
r
\
\
n and thus require
universal newline translation.
"""
name
=
'__benchmark'
with
source_util
.
create_modules
(
name
)
as
mapping
:
with
open
(
mapping
[
name
],
'w'
)
as
file
:
for
x
in
range
(
loc
):
file
.
write
(
"{0}
\
r
\
n
"
.
format
(
x
))
with
util
.
import_state
(
path
=
[
mapping
[
'.root'
]]):
runs
=
[]
for
x
in
range
(
repeat
):
start_time
=
timeit
.
default_timer
()
for
y
in
range
(
number
):
try
:
import_
(
name
)
finally
:
del
sys
.
modules
[
name
]
end_time
=
timeit
.
default_timer
()
runs
.
append
(
end_time
-
start_time
)
return
min
(
runs
)
def
main
(
import_
):
args
=
[(
'sys.modules'
,
bench_cache
,
5
,
500000
),
(
'source'
,
bench_importing_source
,
5
,
10000
)]
test_msg
=
"{test}, {number} times (best of {repeat}):"
result_msg
=
"{result:.2f} secs"
gc
.
disable
()
try
:
for
name
,
meth
,
repeat
,
number
in
args
:
result
=
meth
(
import_
,
repeat
,
number
)
print
(
test_msg
.
format
(
test
=
name
,
repeat
=
repeat
,
number
=
number
).
ljust
(
40
),
result_msg
.
format
(
result
=
result
).
rjust
(
10
))
finally
:
gc
.
enable
()
if
__name__
==
'__main__'
:
import
optparse
parser
=
optparse
.
OptionParser
()
parser
.
add_option
(
'-b'
,
'--builtin'
,
dest
=
'builtin'
,
action
=
'store_true'
,
default
=
False
,
help
=
"use the built-in __import__"
)
options
,
args
=
parser
.
parse_args
()
if
args
:
raise
RuntimeError
(
"unrecognized args: {0}"
.
format
(
args
))
import_
=
__import__
if
not
options
.
builtin
:
import_
=
importlib
.
__import__
main
(
import_
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment