forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_cheat.py
More file actions
executable file
·47 lines (28 loc) · 1.17 KB
/
time_cheat.py
File metadata and controls
executable file
·47 lines (28 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
"""
## time
"""
import time
if '## time()':
# Get number of seconds since 1970:
# Returns a float, thus potentially allowing greater precision,
# but that depends on system support.
# UTC: http://stackoverflow.com/questions/13890935/does-pythons-time-time-return-the-local-or-utc-timestamp
print('time.time() = ' + str(time.time()))
if '## clock':
# Quoting the man: "This is the function to use for benchmarking Python or timing algorithms."
start_time = time.clock()
"-".join(str(n) for n in range(10000))
elapsed_time = time.clock() - start_time
print('clock elapsed = {0:.6f}s'.format(elapsed_time))
# Not accurate for wall time. Does not measure time of external programs.
if '## timeit':
# Benchmark code snippets from strings. Relies internally on either time or clock.
import timeit
print('timeit time of all iterations= {0}s'.format(
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)))
# From the command line:
#python -m timeit '"-".join(str(n) for n in range(100))'
if '## sleep':
"""Sleep for the given number of seconds"""
#time.sleep(3)