Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
9e583f02
Commit
9e583f02
authored
Mar 31, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new micro benchmark for generators
parent
b2b1eeda
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
0 deletions
+76
-0
Demos/benchmarks/generators.py
Demos/benchmarks/generators.py
+76
-0
No files found.
Demos/benchmarks/generators.py
0 → 100644
View file @
9e583f02
#!/usr/bin/python
# micro benchmarks for generators
COUNT
=
10000
def
count_to
(
N
):
for
i
in
range
(
N
):
yield
i
def
round_robin
(
*
iterators
):
iterators
=
list
(
iterators
)
to_drop
=
[]
while
iterators
:
for
i
,
it
in
enumerate
(
iterators
):
try
:
value
=
next
(
it
)
except
StopIteration
:
to_drop
.
append
(
i
)
else
:
yield
value
if
to_drop
:
for
index
in
reversed
(
to_drop
):
del
iterators
[
index
]
del
to_drop
[:]
def
yield_from
(
*
iterators
):
for
it
in
iterators
:
yield
from
it
def
bm_plain
(
N
):
return
count_to
(
COUNT
*
N
)
def
bm_round_robin
(
N
):
return
round_robin
(
*
[
count_to
(
COUNT
//
i
)
for
i
in
range
(
1
,
N
+
1
)
])
def
bm_yield_from
(
N
):
return
yield_from
(
count_to
(
N
),
round_robin
(
*
[
yield_from
(
count_to
(
COUNT
//
i
))
for
i
in
range
(
1
,
N
+
1
)
]),
count_to
(
N
))
def
bm_yield_from_nested
(
N
):
return
yield_from
(
count_to
(
N
),
yield_from
(
count_to
(
N
),
round_robin
(
*
[
yield_from
(
count_to
(
COUNT
//
i
))
for
i
in
range
(
1
,
N
+
1
)
]),
count_to
(
N
)),
count_to
(
N
))
def
time
(
fn
,
*
args
):
from
time
import
time
begin
=
time
()
result
=
list
(
fn
(
*
args
))
end
=
time
()
return
result
,
end
-
begin
def
benchmark
(
N
):
times
=
[]
for
_
in
range
(
N
):
result
,
t
=
time
(
bm_yield_from_nested
,
10
)
times
.
append
(
t
)
return
times
if
__name__
==
"__main__"
:
import
optparse
parser
=
optparse
.
OptionParser
(
usage
=
"%prog [options]"
,
description
=
(
"Micro benchmarks for generators."
))
import
util
util
.
add_standard_options_to
(
parser
)
options
,
args
=
parser
.
parse_args
()
util
.
run_benchmark
(
options
,
options
.
num_runs
,
benchmark
)
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