The max() function of python is written in C, and maths are even faster than a loop
from timeit import timeit from math import floor # 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)
- from timeit import timeit
- from math import floor
- # 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)
import codewars_test as test import solution # or from solution import example @test.describe("Example") def test_group(): @test.it("test case") def test_case(): iterations = 100 list_length = 100000 input_data = [i for i in range(0, list_length)] print(timeit(lambda: sum_even_numbers1(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers2(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers3(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers4(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers5(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers6(input_data), number=iterations))
- import codewars_test as test
- import solution # or from solution import example
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- iterations = 100
- list_length = 100000
- input_data = [i for i in range(0, list_length)]
- print(timeit(lambda: sum_even_numbers1(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers2(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers3(input_data), number=iterations))
print(timeit(lambda: sum_even_numbers4(input_data), number=iterations))- print(timeit(lambda: sum_even_numbers4(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers5(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers6(input_data), number=iterations))