-
Notifications
You must be signed in to change notification settings - Fork 0
/
21. Latency.py
16 lines (10 loc) · 1.2 KB
/
21. Latency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Latency is a measure of time, and there isn't really a specific Python code for measuring it. However, you can use Python to simulate latency in a network by introducing a delay in the transmission of data between two points. Here's an example of how you can do this using the time module:
import time
# Simulate latency by introducing a delay of 1 second
def transmit_data(data):
time.sleep(1) # introduce a 1 second delay
print(f"Data transmitted: {data}")
# Test the function
transmit_data("Hello, world!")
# In this example, the transmit_data() function introduces a delay of 1 second using the time.sleep() function, simulating the delay that might occur when transmitting data over a network with high latency. When the delay is over, the function prints a message indicating that the data has been transmitted.
# Note that this is just a simple example and doesn't take into account the many factors that can affect latency in a real network, such as network congestion, packet loss, and hardware performance. To accurately measure and optimize latency in a real network, you would need to use specialized tools and techniques, such as network monitoring software, traffic shaping, and load testing.