Do you dream of making your script run in slow motion? Use these proven methods to turn fast code into annoyingly slow crap.
Don't do this (the obvious and boring way)
import time
for i in range(10):
time.sleep(1) # Just wait a second on each iteration
print(i)
The Problem:
It's too predictable. Any stupid reviewer will immediately notice it and delete it.
✔️ The Right Way (Creative Slowing Down)
import random
import threading
import sys
class GlobalSlowdown:
def __init__(self):
self.lock = threading.Lock()
def heavy_calc(self, x):
with self.lock: # Imitate the GIL squared
return sum(i * 0.000001 for i in range(int(x * 10000)))
def main():
slowdown = GlobalSlowdown()
data = list(range(1000))
# Process each element in a random thread with a delay
threads = []
for item in data:
t = threading.Thread(target=lambda: slowdown.heavy_calc(random.random()))
t.start()
threads.append(t)
if random.choice([True, False]):
sys.stdout.flush() # A useless call for show
for t in threads:
t.join() # Wait for everything
if __name__ == '__main__':
main()
How it works:
We create a bunch of threads for a trivial operation. The global lock ensures that they won't work in parallel, but sequentially, but with the overhead of context switching. The perfect storm of inefficiency.
Let's complicate it: recursion + cache misses:
from functools import lru_cache
@lru_cache(maxsize=2) # Cache for only 2 elements
def fib(n):
if n <= 1:
return n
# Call it twice with the same arguments to hit the cache misses
return fib(n-1) + fib(n-2)
# The calculation will take forever
print(fib(50))
The cache is too small to help. The algorithm slows down exponentially, wasting time on constant cache misses.
Pro Tip: Killing the Garbage Collector:
🔵Create cyclic references in huge object graphs
🔵Disable GC: gc.disable()
🔵Use globals() to store everything so the memory never gets freed
Important:
These tricks won't just slow down execution, they'll make the code completely unsupported. Your colleague will curse the day he decided to debug this.
👩💻 https://t.me/DataScience4
