Python

Overview

Python is a high-level, interpreted programming language that is popular among developers for its simplicity, readability, and ease of use. Developed in the late 1980s, Python has become one of the most popular programming languages in the world, thanks to its emphasis on code readability, efficient design, and dynamic typing. Python is also open-source, which means that it is free to use, modify, and distribute. It supports various programming paradigms, including procedural, object-oriented, and functional programming, making it a versatile language for a wide range of applications.

Python is often used for data science, web development, machine learning, and scientific computing, among other fields. Its vast array of libraries and frameworks, including NumPy, Pandas, and Django, make it easy to develop complex applications and systems quickly and efficiently. Overall, Python is a popular choice among developers and businesses alike, thanks to its simplicity, versatility, and wide-ranging capabilities.

The objective of this study is to determine the population count for each year by examining birth and death records, with the goal of keeping the methodology straightforward and uncomplicated.

Python

import collections
import random
import timeit

pop = [
    (b := random.randint(1900, 2000), random.randint(b + 1, b + 100))
    for _ in range(10000)
]


# 5.049 s (with 10000 people [1,100] years old)
def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
    living_per_year = {}
    for birth, death in population:
        for year in range(birth, death):
            if year in living_per_year:
                living_per_year[year] += 1
            else:
                living_per_year[year] = 1
    return living_per_year


print(sorted(tuple(count_living_per_year(pop).items())))
print(
    timeit.timeit(
        "count_living_per_year(pop)",
        globals=globals(),
        number=100,
    )
)


# 3.697 s (with 10000 people [1,100] years old)
def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
    living_per_year = {}
    for birth, death in population:
        for year in range(birth, death):
            living_per_year[year] = living_per_year.get(year, 0) + 1
    return living_per_year


print(sorted(tuple(count_living_per_year(pop).items())))
print(
    timeit.timeit(
        "count_living_per_year(pop)",
        globals=globals(),
        number=100,
    )
)


# 3.929 s (with 10000 people [1,100] years old)
def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
    living_per_year = collections.defaultdict(int)
    for birth, death in population:
        for year in range(birth, death):
            living_per_year[year] += 1
    return living_per_year


print(sorted(tuple(count_living_per_year(pop).items())))

print(
    timeit.timeit(
        "count_living_per_year(pop)",
        globals=globals(),
        number=100,
    )
)


# 4.084 s (with 10000 people [1,100] years old)
def count_living_per_year(population: list[tuple[int, int]]) -> dict[int, int]:
    return collections.Counter(
        year for birth, death in population for year in range(birth, death)
    )


print(sorted(tuple(count_living_per_year(pop).items())))

print(
    timeit.timeit(
        "count_living_per_year(pop)",
        globals=globals(),
        number=100,
    )
)

References: