Skip to content

Instantly share code, notes, and snippets.

@jamesmunns
Last active November 4, 2020 02:37
Show Gist options
  • Save jamesmunns/12968ef8b53f7cea87a12d5f7d3b5677 to your computer and use it in GitHub Desktop.
Save jamesmunns/12968ef8b53f7cea87a12d5f7d3b5677 to your computer and use it in GitHub Desktop.
import random
DEVICES = 32
WINDOW_US = 100
FIXED_ROUND_DELAY_SEC = 0.003
# Less than 8 is bad
WINDOWS = 8
print(f"{DEVICES} devices, {WINDOW_US}us per window, {FIXED_ROUND_DELAY_SEC * 1000}ms delay each round")
print("Time Slots, Average Rounds Taken, Average time taken (ms)")
while WINDOWS <= 1024:
rounds_taken = []
for x in range(4096):
registered = 0
rounds = 0
while registered < DEVICES:
rounds += 1
slots = []
collisions = []
unique = []
to_register = DEVICES - registered
for i in range(to_register):
slots.append(random.randrange(WINDOWS))
# print(f"slots: {slots}")
for i in slots:
if i in collisions:
pass
elif i in unique:
collisions.append(i)
while i in unique:
unique.remove(i)
else:
unique.append(i)
registered += len(unique)
# print("Round ended.")
# print(f"Registered: {len(unique)}")
# print(f"Unique collisions: {len(collisions)}")
# print(f"remaining: {DEVICES - registered}")
# print(f"complete. Rounds: {rounds}")
rounds_taken.append(rounds)
avg_rounds = sum(rounds_taken)/len(rounds_taken)
print(f"{WINDOWS}, {avg_rounds:0.03f}, {(((WINDOW_US / 1000000) * avg_rounds) + (avg_rounds * FIXED_ROUND_DELAY_SEC)) * 1000:0.03f}")
WINDOWS *= 2
@jamesmunns
Copy link
Author

32 devices, 100us per window, 3.0ms delay each round
Time Slots, Average Rounds Taken, Average time taken (ms)
8, 23.432, 72.638
16, 6.638, 20.578
32, 3.773, 11.697
64, 2.707, 8.390
128, 2.152, 6.671
256, 1.893, 5.867
512, 1.626, 5.042
1024, 1.389, 4.307

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment