September 2019
Beginner to intermediate
494 pages
13h
English
For this discussion, we will be considering the example that's included in the Chapter06/Profiling subfolder of this book's repository. This subfolder contains a main.py script, which provides the following code:
def custom_sum(n=1000000): result = 0 for i in range(n): result += i return resultdef built_sum(n=1000000): result = sum(range(n)) return resultif __name__ == '__main__': print(custom_sum()) #print(built_sum())
As you can probably guess from the code, we will be comparing and contrasting the two ways of computing the sum of the integers going from one to a specific n (whose default value is 1,000,000). The first way (the custom_sum() function) is to loop through all the elements to be summed and add them to a ...