<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">>
 Summary: Python's timeit.timeit() has an undocumented feature /</span><br style=" color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255)">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">>
 implementation detail that gives much of what the original poster has</span><br style=" color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255)">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">>
 asked for. Perhaps revising the docs will solve the problem.</span><br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important"><br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">although
 timeit can be used with a callable, you need to create a lambda expression if the function has args:</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">```</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">def
 func_to_time(a, b):</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important"> 
   ...</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important"><br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">timeit.timeit(lambda:
 func_to_time(a, b), globals=globals())</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">```</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(33, 33, 33); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">and
 you can't use it as a decorator.</span></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>De :</b> Python-ideas <python-ideas-bounces+amjadhedhili=outlook.com@python.org> de la part de Jonathan Fine <jfine2358@gmail.com><br>
<b>Envoyé :</b> dimanche 7 octobre 2018 09:15<br>
<b>À :</b> python-ideas<br>
<b>Objet :</b> Re: [Python-ideas] add a time decorator to timeit.py</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Summary: Python's timeit.timeit() has an undocumented feature /<br>
implementation detail that gives much of what the original poster has<br>
asked for. Perhaps revising the docs will solve the problem.<br>
<br>
This thread has prompted me to look at timeit again. Usually, I look<br>
at the command line help first.<br>
<br>
>>> import timeit<br>
>>> help(timeit)<br>
    Classes:<br>
        Timer<br>
    Functions:<br>
        timeit(string, string) -> float<br>
        repeat(string, string) -> list<br>
        default_timer() -> float<br>
<br>
This time, to my surprise, I found the following works:<br>
<br>
>>> def fn(): return 2 + 2<br>
>>> timeit.timeit(fn)<br>
0.10153918000287376<br>
<br>
Until today, as I recall, I didn't know this.<br>
<br>
Now for: <a href="https://docs.python.org/3/library/timeit.html">https://docs.python.org/3/library/timeit.html</a><br>
<br>
I don't see any examples there, that show that timeit.timeit can take<br>
a callable as its first argument. So my ignorance can, I hope be<br>
forgiven.<br>
<br>
Now for: <a href="https://github.com/python/cpython/blob/3.7/Lib/timeit.py#L100">
https://github.com/python/cpython/blob/3.7/Lib/timeit.py#L100</a><br>
<br>
This contains, for both the stmt and setup parameters, explicit tests such as<br>
<br>
if isinstance(stmt, str):<br>
    # string case<br>
elif callable(stmt):<br>
    # callable case<br>
<br>
So I think it's an undocumented feature, rather than an implementation detail.<br>
<br>
And if you're a software historian, now perhaps look at<br>
<a href="https://github.com/python/cpython/commits/3.7/Lib/timeit.py">https://github.com/python/cpython/commits/3.7/Lib/timeit.py</a><br>
<br>
And also, if you wish, for the tests for timeit.py.<br>
<br>
-- <br>
Jonathan<br>
_______________________________________________<br>
Python-ideas mailing list<br>
Python-ideas@python.org<br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a><br>
</div>
</span></font></div>
</body>
</html>