# 100 days of code: Day 8 - memory and time

Source: 100-days-of-code.

Working from yesterday’s program I created a more simple program - sum_integers.py- that uses a for loop to generate a list of all numbers between START and STOP and the sum total of all the numbers in the range …

``````# Sum total of all integers between START and STOP.
import time

# Start and stop numbers for range().
start, stop = 0, 0

print("Add together all integers between START and STOP.")
start = int(input("START number? > "))
stop = int(input("STOP number? > "))

start_time = time.time()    # let's time how long each method takes to run
numbers = list(range(start, int(stop +1)))  # range() stops at number minus 1
total_count = sum(numbers)
total_time = time.time() - start_time       # total elapsed time

print(f"\nSum total of all numbers between {start} and " + f"{stop} "
+ f"is {total_count:_}"    # display large numbers with `_` separator
+ ".")
print(f"Calculated in {total_time:.2f} "   # display time to 2 decimal places
+ "seconds.")``````

My laptop running this code has 8GB of RAM and solid state storage. Setting a large STOP value in the hundreds of millions consumes gigabytes of memory. When the program reaches the limits of installed RAM and starts using swap memory, a comparatively small increase in STOP value leads to a dramatic increase in code execution time.

Generating a list and sum from a STOP value of 186.5 million takes less than 6 seconds …

``````\$ python3 sum_integers.py
Add together all integers between START and STOP.
START number? > 1
STOP number? > 186500000

Sum total of all numbers between 1 and 186500000 is 17_391_125_093_250_000.
Calculated in 5.49 seconds.``````

An increase of 100 thousand (0.0005%) shows over a 1000% increase in execution time!

``````\$ python3 sum_integers.py
Add together all integers between START and STOP.
START number? > 1
STOP number? > 186600000

Sum total of all numbers between 1 and 186600000 is 17_409_780_093_300_000.
Calculated in 58.28 seconds.``````

Wow.

Happy hacking!