-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime-module.py
More file actions
65 lines (42 loc) · 1.17 KB
/
Copy pathtime-module.py
File metadata and controls
65 lines (42 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# The time module provides functions for working with time — getting the current time,
# measuring durations, pausing execution, and formatting timestamps.
# It's part of Python's standard library, so no installation needed.
# time.time() returns a float representing seconds since the epoch.
# It's commonly used for timing code or logging.
import time
def usingwhile():
i = 0
while i < 50000:
i += 1
print(i)
def usingfor():
for i in range(50000):
print(i)
init = time.time()
usingwhile()
print(time.time() - init)
usingfor()
print(time.time() - init)
-----------------------------
-----------------------------
import time
t = time.time()
print(t) # 1753776000.123456 (seconds since Jan 1, 1970 - the "epoch")
-----------------------------
-----------------------------
import time
t = time.time()
print("Start time:", t)
time.sleep(5)
t2 = time.time()
print("End time:", t2)
start = time.time()
# ... do some work ...
end = time.time()
--------------------------
--------------------------
#formatted time:
import time
t = time.localtime()
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", t)
print("Current time:", formatted_time)