Skip to content

Instantly share code, notes, and snippets.

@HDCharles
Last active January 8, 2026 04:44
Show Gist options
  • Select an option

  • Save HDCharles/2f15d2b383c4da9251a7245e58d3ac47 to your computer and use it in GitHub Desktop.

Select an option

Save HDCharles/2f15d2b383c4da9251a7245e58d3ac47 to your computer and use it in GitHub Desktop.
test kyle thing
import random
random.seed(0)
from collections import deque
import math
import time
### SET UP PROBLEM
l, h = 100,100000
delta = 10
tests = [
[1000, 2],
[1000, 4],
[1000, 8],
[1000, 16],
[100000, 2],
[100000, 4],
[100000, 8],
[100000, 16],
[100000, 32],
[1000000, 2],
[1000000, 4],
[1000000, 8],
[1000000, 16],
]
deltas = []
for num_modules, num_devices in tests:
print(f"running comparison with for mods={num_modules}, devs={num_devices}")
avg_d = int(num_modules*1.25*(h+l)/2/num_devices+1)
module_sizes = [0]
device_sizes = [-1]
while sum(module_sizes) > sum(device_sizes):
module_sizes = [random.randint(l, h) for i in range(num_modules)]
device_sizes = [random.randint(avg_d-delta, avg_d+delta) for i in range(num_devices)]
### SOLVE PROBLEM USING BINARY SEARCH
s = time.time()
def canallocate(module_sizes, device_sizes, target):
i = 0
for dev in device_sizes:
filled = 0
while i<len(module_sizes) and filled + module_sizes[i] <= dev - target:
filled += module_sizes[i]
i+=1
return i==len(module_sizes)
low, high = -1, min(device_sizes)
while high - low > 1:
mid = (low + high)//2
if canallocate(module_sizes, device_sizes, mid):
low = mid
else:
high = mid
res = time.time()-s
print(f" kylestuff took: {res}")
s = time.time()
### SOLVE PROBLEM USING RIGHT SHIFTING AND SEGMENT TREE
module_allocation = deque()
i = 0
for dev in device_sizes:
module_allocation.append(deque())
filled = 0
while i<num_modules and filled + module_sizes[i] <= dev:
module_allocation[-1].append(module_sizes[i])
filled += module_sizes[i]
i+=1
device_space = [dev - sum(mods) for mods, dev in zip(module_allocation, device_sizes)]
def update(tree, index, value):
j = index + len(tree)//2
tree[j] = [value, index]
while j!=0:
j = (j-1)//2
tree[j] = min(tree[2*j+1], tree[2*j+2])
def initialize(values):
n = len(values)
m = 2**(math.ceil(math.log2(n)))
tree = [[]]*(2*n-1)
for i in list(range(n))[::-1]:
update(tree, i, values[i])
return tree
def moveright(module_allocation, tree, index):
n = len(tree)//2
module_allocation[index+1].appendleft(val:=module_allocation[index].pop())
update(tree, index, tree[index + n][0] + val)
update(tree, index+1, tree[n + index+1][0] - val)
tree = initialize(device_space)
best = -100
allocation = list(module_allocation)
while True:
if tree[0][0] > best:
best = tree[0][0]
allocation = list(module_allocation)
worst = tree[0][1]
if worst == num_devices-1:
break
moveright(module_allocation, tree, worst)
res2 = time.time()-s
print(f" rightshift took: {res2}")
deltas.append(res2-res)
print("deltas:", deltas)
@HDCharles
Copy link
Author

HDCharles commented Jan 8, 2026

running comparison with for mods=1000, devs=2
 kylestuff took: 0.0022420883178710938
 rightshift took: 0.00039124488830566406
running comparison with for mods=1000, devs=4
 kylestuff took: 0.0020508766174316406
 rightshift took: 0.0007145404815673828
running comparison with for mods=1000, devs=8
 kylestuff took: 0.0020961761474609375
 rightshift took: 0.001394510269165039
running comparison with for mods=1000, devs=16
 kylestuff took: 0.002022981643676758
 rightshift took: 0.0028672218322753906
running comparison with for mods=100000, devs=2
 kylestuff took: 0.30698132514953613
 rightshift took: 0.03232765197753906
running comparison with for mods=100000, devs=4
 kylestuff took: 0.29709386825561523
 rightshift took: 0.06917309761047363
running comparison with for mods=100000, devs=8
 kylestuff took: 0.27221035957336426
 rightshift took: 0.14147710800170898
running comparison with for mods=100000, devs=16
 kylestuff took: 0.30924129486083984
 rightshift took: 0.31145191192626953
running comparison with for mods=100000, devs=32
 kylestuff took: 0.24469208717346191
 rightshift took: 0.7029907703399658
running comparison with for mods=1000000, devs=2
 kylestuff took: 3.594341516494751
 rightshift took: 0.32824039459228516
running comparison with for mods=1000000, devs=4
 kylestuff took: 3.5787312984466553
 rightshift took: 0.7001583576202393
running comparison with for mods=1000000, devs=8
 kylestuff took: 3.410893440246582
 rightshift took: 1.4324908256530762
running comparison with for mods=1000000, devs=16
 kylestuff took: 3.6177220344543457
 rightshift took: 3.1603851318359375
deltas: [-0.0018508434295654297, -0.0013363361358642578, -0.0007016658782958984, 0.0008442401885986328, -0.27465367317199707, -0.2279207706451416, -0.13073325157165527, 0.0022106170654296875, 0.4582986831665039, -3.266101121902466, -2.878572940826416, -1.9784026145935059, -0.4573369026184082]

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