Changes
Jump to navigation
Jump to search
← Older edit
Python decorators
(edit)
Revision as of 08:46, 14 February 2022
154 bytes added
,
08:46, 14 February 2022
m
→Timeit decorator
Line 1:
Line 1:
−
== Timeit decorator ==
+
==Timeit decorator==
<syntaxhighlight lang="python3">
<syntaxhighlight lang="python3">
import time
import time
Line 5:
Line 5:
def timeit(method):
def timeit(method):
def timed(*args, **kw):
def timed(*args, **kw):
−
ts
= time.time()
+
start_time
= time.time()
result = method(*args, **kw)
result = method(*args, **kw)
−
te
= time.time()
+
end_time
= time.time()
−
print(f'{method.__name__} {(
te
-
ts
) * 1000} ms')
+
print(f'{method.__name__} {(
end_time
-
start_time
) * 1000} ms')
return result
return result
return timed
return timed
+
</syntaxhighlight>
+
+
=== Usage ===
+
<syntaxhighlight lang="python3">
+
@timeit
+
def test_func(arg1: int)->None:
+
time.sleep(arg1)
</syntaxhighlight>
</syntaxhighlight>
Rafahsolis
Bureaucrats
,
Administrators
2,306
edits
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
Variants
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
Special pages
Printable version