Ad
Code
Diff
  • from timeit import timeit
    from math import floor
    import numpy as np
    
    # 1. Can you implement another optimised function (sum_even_numbers2) using purely native python?
    #    Can you timeit to show it's faster and describe why it is.
    #    Can you also state if there are any pros or cons to how it is implemented?
    
    # 2. Can you implement another optimised function (sum_even_numbers3) using third party packages?
    #    Can you timeit to show it's faster and describe why it is.
    #    Can you also state if there are any pros or cons to how it is implemented?
    
    def sum_even_numbers1(numbers: list[int]) -> int:
        total = 0
        for num in numbers:
            if floor(num/2) == num/2:
                total += num
        return total
    
    # Floating point calculation is not required
    # Only one division required
    # float required 32byte, memory heavy
    def sum_even_numbers2(numbers: list[int]) -> int:
        total = 0
        for num in numbers:
            if num%2 == 0:
                total += num
        return total
    
    # needs to call that function from memory
    def sum_even_numbers3(numbers: list[int]) -> int:
        return sum(filter(lambda x: x > 0 and x%2 == 0, numbers))
    
    # generators are often faster
    def sum_even_numbers4(numbers: list[int]) -> int:
        return sum(num for num in numbers if not num%2)
    
    # generators without if statements are almost always faster
    def sum_even_numbers5(numbers: list[int]) -> int:
        return sum(n * (not n&1) for n in numbers)
    
    # the max() function of python is written in C, and maths are even faster than a loop
    def sum_even_numbers6(numbers: list[int]) -> int:
        maxi = max(numbers)
        return maxi//2 * (maxi//2 + 1)
    
    # why keeping two divisions?
    def sum_even_numbers7(numbers: list[int]) -> int:
        return (maxi := max(numbers) >> 1) * (maxi + 1)
    
    # Simplicity: The code is simple and easy to understand.
    # No Floating-Point Operations: This implementation avoids floating-point operations,
    # which can introduce rounding errors.
    def sum_even_numbers8(numbers: list[int]) -> int:
        total = 0
        for num in numbers:
            if num % 2 == 0:
                total += num
        return total
    
    # Conciseness: NumPy provides concise and expressive syntax for array operations.
    # Vectorized Operations: NumPy operations are usually vectorized, making them efficient
    # for large datasets.
    def sum_even_numbers9(numbers: list[int]) -> int:
        return np.sum(np.array(numbers)[np.array(numbers) % 2 == 0])
    
    # bitwise AND operation checks if the least significant bit is 0, indicating an even number
    def sum_even_numbers10(numbers):
        return sum(x for x in numbers if not x & 1)
    
    • from timeit import timeit
    • from math import floor
    • import numpy as np
    • # 1. Can you implement another optimised function (sum_even_numbers2) using purely native python?
    • # Can you timeit to show it's faster and describe why it is.
    • # Can you also state if there are any pros or cons to how it is implemented?
    • # 2. Can you implement another optimised function (sum_even_numbers3) using third party packages?
    • # Can you timeit to show it's faster and describe why it is.
    • # Can you also state if there are any pros or cons to how it is implemented?
    • def sum_even_numbers1(numbers: list[int]) -> int:
    • total = 0
    • for num in numbers:
    • if floor(num/2) == num/2:
    • total += num
    • return total
    • # Floating point calculation is not required
    • # Only one division required
    • # float required 32byte, memory heavy
    • def sum_even_numbers2(numbers: list[int]) -> int:
    • total = 0
    • for num in numbers:
    • if num%2 == 0:
    • total += num
    • return total
    • # needs to call that function from memory
    • def sum_even_numbers3(numbers: list[int]) -> int:
    • return sum(filter(lambda x: x > 0 and x%2 == 0, numbers))
    • # generators are often faster
    • def sum_even_numbers4(numbers: list[int]) -> int:
    • return sum(num for num in numbers if not num%2)
    • # generators without if statements are almost always faster
    • def sum_even_numbers5(numbers: list[int]) -> int:
    • return sum(n * (not n&1) for n in numbers)
    • # the max() function of python is written in C, and maths are even faster than a loop
    • def sum_even_numbers6(numbers: list[int]) -> int:
    • maxi = max(numbers)
    • return maxi//2 * (maxi//2 + 1)
    • # why keeping two divisions?
    • def sum_even_numbers7(numbers: list[int]) -> int:
    • return (maxi := max(numbers) >> 1) * (maxi + 1)
    • return (maxi := max(numbers) >> 1) * (maxi + 1)
    • # Simplicity: The code is simple and easy to understand.
    • # No Floating-Point Operations: This implementation avoids floating-point operations,
    • # which can introduce rounding errors.
    • def sum_even_numbers8(numbers: list[int]) -> int:
    • total = 0
    • for num in numbers:
    • if num % 2 == 0:
    • total += num
    • return total
    • # Conciseness: NumPy provides concise and expressive syntax for array operations.
    • # Vectorized Operations: NumPy operations are usually vectorized, making them efficient
    • # for large datasets.
    • def sum_even_numbers9(numbers: list[int]) -> int:
    • return np.sum(np.array(numbers)[np.array(numbers) % 2 == 0])
    • # bitwise AND operation checks if the least significant bit is 0, indicating an even number
    • def sum_even_numbers10(numbers):
    • return sum(x for x in numbers if not x & 1)