VikkaLorel

living per year

Apr 16th, 2023
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. import collections
  2. import random
  3. import timeit
  4.  
  5. pop = [
  6.     (b := random.randint(1900, 2000), random.randint(b + 1, b + 100))
  7.     for _ in range(10000)
  8. ]
  9.  
  10.  
  11. # 5.049 s (with 10000 people [1,100] years old)
  12. def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
  13.     living_per_year = {}
  14.     for birth, death in population:
  15.         for year in range(birth, death + 1):
  16.             if year in living_per_year:
  17.                 living_per_year[year] += 1
  18.             else:
  19.                 living_per_year[year] = 1
  20.     return living_per_year
  21.  
  22.  
  23. print(
  24.     timeit.timeit(
  25.         "count_living_per_year(pop)",
  26.         globals=globals(),
  27.         number=100,
  28.     )
  29. )
  30.  
  31.  
  32. # 3.697 s (with 10000 people [1,100] years old)
  33. def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
  34.     living_per_year = {}
  35.     for birth, death in population:
  36.         for year in range(birth, death + 1):
  37.             living_per_year[year] = living_per_year.get(year, 0) + 1
  38.     return living_per_year
  39.  
  40.  
  41. print(
  42.     timeit.timeit(
  43.         "count_living_per_year(pop)",
  44.         globals=globals(),
  45.         number=100,
  46.     )
  47. )
  48.  
  49.  
  50. # 3.929 s (with 10000 people [1,100] years old)
  51. def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
  52.     living_per_year = collections.defaultdict(int)
  53.     for birth, death in population:
  54.         for year in range(birth, death + 1):
  55.             living_per_year[year] += 1
  56.     return living_per_year
  57.  
  58.  
  59. print(
  60.     timeit.timeit(
  61.         "count_living_per_year(pop)",
  62.         globals=globals(),
  63.         number=100,
  64.     )
  65. )
  66.  
  67.  
  68. # 4.084 s (with 10000 people [1,100] years old)
  69. def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
  70.     return collections.Counter(
  71.         year for birth, death in population for year in range(birth, death + 1)
  72.     )
  73.  
  74.  
  75. print(sorted(tuple(count_living_per_year(pop).items())))
  76.  
  77. print(
  78.     timeit.timeit(
  79.         "count_living_per_year(pop)",
  80.         globals=globals(),
  81.         number=100,
  82.     )
  83. )
  84.  
  85.  
  86. # 0.145 s (with 10000 people [1,100] years old)
  87. def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
  88.     births = collections.Counter(birth for birth, _ in population)
  89.     deaths = collections.Counter(death for _, death in population)
  90.     living_per_year = births.copy()
  91.     for year in range(min(births), max(deaths) + 1):
  92.         living_per_year[year] += living_per_year[year - 1] - deaths[year - 1]
  93.     return living_per_year
  94.  
  95.  
  96. print(sorted(tuple(count_living_per_year(pop).items())))
  97. print(
  98.     timeit.timeit(
  99.         "count_living_per_year(pop)",
  100.         globals=globals(),
  101.         number=100,
  102.     )
  103. )
  104.  
  105. # 0.152 s (with 10000 people [1,100] years old)
  106. def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
  107.     living_per_year = collections.defaultdict(int)
  108.     for birth, death in population:
  109.         living_per_year[birth] += 1
  110.         living_per_year[death + 1] -= 1
  111.     min_, max_ = min(living_per_year), max(living_per_year)
  112.     for year in range(min_ + 1, max_ + 1):
  113.         living_per_year[year] += living_per_year[year - 1]
  114.     del living_per_year[max_]
  115.     return living_per_year
  116.  
  117.  
  118. print(sorted(tuple(count_living_per_year(pop).items())))
  119. print(
  120.     timeit.timeit(
  121.         "count_living_per_year(pop)",
  122.         globals=globals(),
  123.         number=100,
  124.     )
  125. )
  126.  
  127. # 0.146 s (with 10000 people [1,100] years old)
  128. def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
  129.     births = collections.Counter(birth for birth, _ in population)
  130.     deaths = collections.Counter(death for _, death in population)
  131.     (delta_per_year := births).subtract(deaths)
  132.     living_per_year = collections.Counter()
  133.     for year in range(min(delta_per_year), max(delta_per_year) + 1):
  134.         living_per_year[year] = living_per_year[year - 1] + delta_per_year[year]
  135.     return living_per_year
  136.  
  137.  
  138. print(sorted(tuple(count_living_per_year(pop).items())))
  139. print(
  140.     timeit.timeit(
  141.         "count_living_per_year(pop)",
  142.         globals=globals(),
  143.         number=100,
  144.     )
  145. )
  146.  
Advertisement
Add Comment
Please, Sign In to add comment