fork download
  1. from threading import Timer
  2. from queue import Queue
  3. from random import shuffle
  4. from time import time
  5.  
  6. def is_sorted(l):
  7. return all(a <= b for a, b in zip(l[:-1], l[1:]))
  8.  
  9. limit = 10
  10. m = 0
  11. q = Queue()
  12. sorted = False
  13.  
  14. while not sorted:
  15. m += 1
  16. start = time()
  17. l = list(range(1,limit+1))
  18. shuffle(l)
  19.  
  20. for n in l:
  21. Timer(n * m / 1.0e3, lambda: q.put(n)).start()
  22.  
  23. sorted = is_sorted([q.get() for i in range(limit)])
  24. print("limit,{0}; m,{1}; time(sec),{2}; sorted,{3}".format(limit,m,time()-start,sorted))
  25.  
  26.  
  27. # limit,100; m,1; time(sec),0.1221776008605957; sorted,False
  28. # limit,100; m,2; time(sec),0.2172226905822754; sorted,False
  29. # limit,100; m,3; time(sec),0.3354020118713379; sorted,False
  30. # limit,100; m,4; time(sec),0.45334887504577637; sorted,False
  31. # limit,100; m,5; time(sec),0.5745019912719727; sorted,False
  32. # limit,100; m,6; time(sec),0.6506564617156982; sorted,False
  33. # limit,100; m,7; time(sec),0.7495026588439941; sorted,False
  34. # limit,100; m,8; time(sec),0.8507623672485352; sorted,False
  35. # limit,100; m,9; time(sec),0.9376990795135498; sorted,False
  36. # limit,100; m,10; time(sec),1.0557076930999756; sorted,False
  37. # limit,100; m,11; time(sec),1.1280319690704346; sorted,False
  38. # limit,100; m,12; time(sec),1.2193403244018555; sorted,True
Success #stdin #stdout 0.04s 716288KB
stdin
Standard input is empty
stdout
limit,10; m,1; time(sec),0.010822534561157227; sorted,True