Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
d3ff784f
Kaydet (Commit)
d3ff784f
authored
Eki 23, 2016
tarafından
Serhiy Storchaka
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #28469: timeit now uses the sequence 1, 2, 5, 10, 20, 50,... instead
of 1, 10, 100,... for autoranging.
üst
f8d7d415
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
49 deletions
+65
-49
timeit.rst
Doc/library/timeit.rst
+11
-12
test_timeit.py
Lib/test/test_timeit.py
+38
-25
timeit.py
Lib/timeit.py
+13
-12
NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/timeit.rst
Dosyayı görüntüle @
d3ff784f
...
@@ -28,11 +28,11 @@ can be used to compare three different expressions:
...
@@ -28,11 +28,11 @@ can be used to compare three different expressions:
.. code-block:: sh
.. code-block:: sh
$ python3 -m timeit '"-".join(str(n) for n in range(100))'
$ python3 -m timeit '"-".join(str(n) for n in range(100))'
10000 loops, best of
3
: 30.2 usec per loop
10000 loops, best of
5
: 30.2 usec per loop
$ python3 -m timeit '"-".join([str(n) for n in range(100)])'
$ python3 -m timeit '"-".join([str(n) for n in range(100)])'
10000 loops, best of
3
: 27.5 usec per loop
10000 loops, best of
5
: 27.5 usec per loop
$ python3 -m timeit '"-".join(map(str, range(100)))'
$ python3 -m timeit '"-".join(map(str, range(100)))'
10000 loops, best of
3
: 23.2 usec per loop
10000 loops, best of
5
: 23.2 usec per loop
This can be achieved from the :ref:`python-interface` with::
This can be achieved from the :ref:`python-interface` with::
...
@@ -141,9 +141,8 @@ The module defines three convenience functions and a public class:
...
@@ -141,9 +141,8 @@ The module defines three convenience functions and a public class:
This is a convenience function that calls :meth:`.timeit` repeatedly
This is a convenience function that calls :meth:`.timeit` repeatedly
so that the total time >= 0.2 second, returning the eventual
so that the total time >= 0.2 second, returning the eventual
(number of loops, time taken for that number of loops). It calls
(number of loops, time taken for that number of loops). It calls
:meth:`.timeit` with *number* set to successive powers of ten (10,
:meth:`.timeit` with increasing numbers from the sequence 1, 2, 5,
100, 1000, ...) up to a maximum of one billion, until the time taken
10, 20, 50, ... until the time taken is at least 0.2 second.
is at least 0.2 second, or the maximum is reached.
If *callback* is given and is not ``None``, it will be called after
If *callback* is given and is not ``None``, it will be called after
each trial with two arguments: ``callback(number, time_taken)``.
each trial with two arguments: ``callback(number, time_taken)``.
...
@@ -268,9 +267,9 @@ It is possible to provide a setup statement that is executed only once at the be
...
@@ -268,9 +267,9 @@ It is possible to provide a setup statement that is executed only once at the be
.. code-block:: sh
.. code-block:: sh
$ python -m timeit -s 'text = "sample string"; char = "g"' 'char in text'
$ python -m timeit -s 'text = "sample string"; char = "g"' 'char in text'
10000000 loops, best of 3
: 0.0877 usec per loop
5000000 loops, best of 5
: 0.0877 usec per loop
$ python -m timeit -s 'text = "sample string"; char = "g"' 'text.find(char)'
$ python -m timeit -s 'text = "sample string"; char = "g"' 'text.find(char)'
1000000 loops, best of
3
: 0.342 usec per loop
1000000 loops, best of
5
: 0.342 usec per loop
::
::
...
@@ -297,14 +296,14 @@ to test for missing and present object attributes:
...
@@ -297,14 +296,14 @@ to test for missing and present object attributes:
.. code-block:: sh
.. code-block:: sh
$ python -m timeit 'try:' ' str.__bool__' 'except AttributeError:' ' pass'
$ python -m timeit 'try:' ' str.__bool__' 'except AttributeError:' ' pass'
100000 loops, best of 3
: 15.7 usec per loop
20000 loops, best of 5
: 15.7 usec per loop
$ python -m timeit 'if hasattr(str, "__bool__"): pass'
$ python -m timeit 'if hasattr(str, "__bool__"): pass'
100000 loops, best of 3
: 4.26 usec per loop
50000 loops, best of 5
: 4.26 usec per loop
$ python -m timeit 'try:' ' int.__bool__' 'except AttributeError:' ' pass'
$ python -m timeit 'try:' ' int.__bool__' 'except AttributeError:' ' pass'
1000000 loops, best of 3
: 1.43 usec per loop
200000 loops, best of 5
: 1.43 usec per loop
$ python -m timeit 'if hasattr(int, "__bool__"): pass'
$ python -m timeit 'if hasattr(int, "__bool__"): pass'
100000 loops, best of
3
: 2.23 usec per loop
100000 loops, best of
5
: 2.23 usec per loop
::
::
...
...
Lib/test/test_timeit.py
Dosyayı görüntüle @
d3ff784f
...
@@ -256,7 +256,7 @@ class TestTimeit(unittest.TestCase):
...
@@ -256,7 +256,7 @@ class TestTimeit(unittest.TestCase):
def
test_main_milliseconds
(
self
):
def
test_main_milliseconds
(
self
):
s
=
self
.
run_main
(
seconds_per_increment
=
0.0055
)
s
=
self
.
run_main
(
seconds_per_increment
=
0.0055
)
self
.
assertEqual
(
s
,
"
10
0 loops, best of 5: 5.5 msec per loop
\n
"
)
self
.
assertEqual
(
s
,
"
5
0 loops, best of 5: 5.5 msec per loop
\n
"
)
def
test_main_microseconds
(
self
):
def
test_main_microseconds
(
self
):
s
=
self
.
run_main
(
seconds_per_increment
=
0.0000025
,
switches
=
[
'-n100'
])
s
=
self
.
run_main
(
seconds_per_increment
=
0.0000025
,
switches
=
[
'-n100'
])
...
@@ -304,35 +304,43 @@ class TestTimeit(unittest.TestCase):
...
@@ -304,35 +304,43 @@ class TestTimeit(unittest.TestCase):
"""
))
"""
))
def
test_main_very_verbose
(
self
):
def
test_main_very_verbose
(
self
):
s
=
self
.
run_main
(
seconds_per_increment
=
0.000
05
0
,
switches
=
[
'-vv'
])
s
=
self
.
run_main
(
seconds_per_increment
=
0.000
_03
0
,
switches
=
[
'-vv'
])
self
.
assertEqual
(
s
,
dedent
(
"""
\
self
.
assertEqual
(
s
,
dedent
(
"""
\
1 loop -> 5e-05 secs
1 loop -> 3e-05 secs
10 loops -> 0.0005 secs
2 loops -> 6e-05 secs
100 loops -> 0.005 secs
5 loops -> 0.00015 secs
1000 loops -> 0.05 secs
10 loops -> 0.0003 secs
10000 loops -> 0.5 secs
20 loops -> 0.0006 secs
50 loops -> 0.0015 secs
raw times: 500 msec, 500 msec, 500 msec, 500 msec, 500 msec
100 loops -> 0.003 secs
200 loops -> 0.006 secs
10000 loops, best of 5: 50 usec per loop
500 loops -> 0.015 secs
1000 loops -> 0.03 secs
2000 loops -> 0.06 secs
5000 loops -> 0.15 secs
10000 loops -> 0.3 secs
raw times: 300 msec, 300 msec, 300 msec, 300 msec, 300 msec
10000 loops, best of 5: 30 usec per loop
"""
))
"""
))
def
test_main_with_time_unit
(
self
):
def
test_main_with_time_unit
(
self
):
unit_sec
=
self
.
run_main
(
seconds_per_increment
=
0.00
2
,
unit_sec
=
self
.
run_main
(
seconds_per_increment
=
0.00
3
,
switches
=
[
'-u'
,
'sec'
])
switches
=
[
'-u'
,
'sec'
])
self
.
assertEqual
(
unit_sec
,
self
.
assertEqual
(
unit_sec
,
"100 loops, best of 5: 0.00
2
sec per loop
\n
"
)
"100 loops, best of 5: 0.00
3
sec per loop
\n
"
)
unit_msec
=
self
.
run_main
(
seconds_per_increment
=
0.00
2
,
unit_msec
=
self
.
run_main
(
seconds_per_increment
=
0.00
3
,
switches
=
[
'-u'
,
'msec'
])
switches
=
[
'-u'
,
'msec'
])
self
.
assertEqual
(
unit_msec
,
self
.
assertEqual
(
unit_msec
,
"100 loops, best of 5:
2
msec per loop
\n
"
)
"100 loops, best of 5:
3
msec per loop
\n
"
)
unit_usec
=
self
.
run_main
(
seconds_per_increment
=
0.00
2
,
unit_usec
=
self
.
run_main
(
seconds_per_increment
=
0.00
3
,
switches
=
[
'-u'
,
'usec'
])
switches
=
[
'-u'
,
'usec'
])
self
.
assertEqual
(
unit_usec
,
self
.
assertEqual
(
unit_usec
,
"100 loops, best of 5:
2
e+03 usec per loop
\n
"
)
"100 loops, best of 5:
3
e+03 usec per loop
\n
"
)
# Test invalid unit input
# Test invalid unit input
with
captured_stderr
()
as
error_stringio
:
with
captured_stderr
()
as
error_stringio
:
invalid
=
self
.
run_main
(
seconds_per_increment
=
0.00
2
,
invalid
=
self
.
run_main
(
seconds_per_increment
=
0.00
3
,
switches
=
[
'-u'
,
'parsec'
])
switches
=
[
'-u'
,
'parsec'
])
self
.
assertEqual
(
error_stringio
.
getvalue
(),
self
.
assertEqual
(
error_stringio
.
getvalue
(),
"Unrecognized unit. Please select nsec, usec, msec, or sec.
\n
"
)
"Unrecognized unit. Please select nsec, usec, msec, or sec.
\n
"
)
...
@@ -347,15 +355,15 @@ class TestTimeit(unittest.TestCase):
...
@@ -347,15 +355,15 @@ class TestTimeit(unittest.TestCase):
s
=
self
.
run_main
(
switches
=
[
'-n1'
,
'1/0'
])
s
=
self
.
run_main
(
switches
=
[
'-n1'
,
'1/0'
])
self
.
assert_exc_string
(
error_stringio
.
getvalue
(),
'ZeroDivisionError'
)
self
.
assert_exc_string
(
error_stringio
.
getvalue
(),
'ZeroDivisionError'
)
def
autorange
(
self
,
seconds_per_increment
=
0.001
,
callback
=
None
):
def
autorange
(
self
,
seconds_per_increment
=
1
/
1024
,
callback
=
None
):
timer
=
FakeTimer
(
seconds_per_increment
=
seconds_per_increment
)
timer
=
FakeTimer
(
seconds_per_increment
=
seconds_per_increment
)
t
=
timeit
.
Timer
(
stmt
=
self
.
fake_stmt
,
setup
=
self
.
fake_setup
,
timer
=
timer
)
t
=
timeit
.
Timer
(
stmt
=
self
.
fake_stmt
,
setup
=
self
.
fake_setup
,
timer
=
timer
)
return
t
.
autorange
(
callback
)
return
t
.
autorange
(
callback
)
def
test_autorange
(
self
):
def
test_autorange
(
self
):
num_loops
,
time_taken
=
self
.
autorange
()
num_loops
,
time_taken
=
self
.
autorange
()
self
.
assertEqual
(
num_loops
,
10
00
)
self
.
assertEqual
(
num_loops
,
5
00
)
self
.
assertEqual
(
time_taken
,
1.0
)
self
.
assertEqual
(
time_taken
,
500
/
1024
)
def
test_autorange_second
(
self
):
def
test_autorange_second
(
self
):
num_loops
,
time_taken
=
self
.
autorange
(
seconds_per_increment
=
1.0
)
num_loops
,
time_taken
=
self
.
autorange
(
seconds_per_increment
=
1.0
)
...
@@ -367,12 +375,17 @@ class TestTimeit(unittest.TestCase):
...
@@ -367,12 +375,17 @@ class TestTimeit(unittest.TestCase):
print
(
"{} {:.3f}"
.
format
(
a
,
b
))
print
(
"{} {:.3f}"
.
format
(
a
,
b
))
with
captured_stdout
()
as
s
:
with
captured_stdout
()
as
s
:
num_loops
,
time_taken
=
self
.
autorange
(
callback
=
callback
)
num_loops
,
time_taken
=
self
.
autorange
(
callback
=
callback
)
self
.
assertEqual
(
num_loops
,
10
00
)
self
.
assertEqual
(
num_loops
,
5
00
)
self
.
assertEqual
(
time_taken
,
1.0
)
self
.
assertEqual
(
time_taken
,
500
/
1024
)
expected
=
(
'1 0.001
\n
'
expected
=
(
'1 0.001
\n
'
'2 0.002
\n
'
'5 0.005
\n
'
'10 0.010
\n
'
'10 0.010
\n
'
'100 0.100
\n
'
'20 0.020
\n
'
'1000 1.000
\n
'
)
'50 0.049
\n
'
'100 0.098
\n
'
'200 0.195
\n
'
'500 0.488
\n
'
)
self
.
assertEqual
(
s
.
getvalue
(),
expected
)
self
.
assertEqual
(
s
.
getvalue
(),
expected
)
...
...
Lib/timeit.py
Dosyayı görüntüle @
d3ff784f
...
@@ -208,22 +208,23 @@ class Timer:
...
@@ -208,22 +208,23 @@ class Timer:
def
autorange
(
self
,
callback
=
None
):
def
autorange
(
self
,
callback
=
None
):
"""Return the number of loops so that total time >= 0.2.
"""Return the number of loops so that total time >= 0.2.
Calls the timeit method with *number* set to successive powers of
Calls the timeit method with increasing numbers from the sequence
ten (10, 100, 1000, ...) up to a maximum of one billion, until
1, 2, 5, 10, 20, 50, ... until the time taken is at least 0.2
the time taken is at least 0.2 second, or the maximum is reached.
second. Returns (number, time_taken).
Returns ``(number, time_taken)``.
If *callback* is given and is not None, it will be called after
If *callback* is given and is not None, it will be called after
each trial with two arguments: ``callback(number, time_taken)``.
each trial with two arguments: ``callback(number, time_taken)``.
"""
"""
for
i
in
range
(
0
,
10
):
i
=
1
number
=
10
**
i
while
True
:
time_taken
=
self
.
timeit
(
number
)
for
j
in
1
,
2
,
5
:
if
callback
:
number
=
i
*
j
callback
(
number
,
time_taken
)
time_taken
=
self
.
timeit
(
number
)
if
time_taken
>=
0.2
:
if
callback
:
break
callback
(
number
,
time_taken
)
return
(
number
,
time_taken
)
if
time_taken
>=
0.2
:
return
(
number
,
time_taken
)
i
*=
10
def
timeit
(
stmt
=
"pass"
,
setup
=
"pass"
,
timer
=
default_timer
,
def
timeit
(
stmt
=
"pass"
,
setup
=
"pass"
,
timer
=
default_timer
,
number
=
default_number
,
globals
=
None
):
number
=
default_number
,
globals
=
None
):
...
...
Misc/NEWS
Dosyayı görüntüle @
d3ff784f
...
@@ -97,6 +97,9 @@ Core and Builtins
...
@@ -97,6 +97,9 @@ Core and Builtins
Library
Library
-------
-------
- Issue #28469: timeit now uses the sequence 1, 2, 5, 10, 20, 50,... instead
of 1, 10, 100,... for autoranging.
- Issue #28115: Command-line interface of the zipfile module now uses argparse.
- Issue #28115: Command-line interface of the zipfile module now uses argparse.
Added support of long options.
Added support of long options.
...
...
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