fork download
  1. from timeit import timeit
  2. import sys
  3.  
  4. N = 3000000
  5.  
  6. def build_set():
  7. x = set()
  8. for i in range(N):
  9. x.add(i)
  10. print('size of the set in bytes:', sys.getsizeof(x))
  11.  
  12. def build_list():
  13. x = []
  14. for i in range(N):
  15. x.append(i)
  16. print('size of the list in bytes:', sys.getsizeof(x))
  17.  
  18. print('time for building the set:', timeit(build_set, number=1))
  19. print('time for building the set:', timeit(build_list, number=1))
  20.  
Success #stdin #stdout 1.36s 9032KB
stdin
Standard input is empty
stdout
size of the set in bytes: 67108976
time for building the set: 0.8012551907449961
size of the list in bytes: 12552996
time for building the set: 0.5374550092965364