Optimize an existing Python code to reduce the response time.
"""
Original Python code:

import time

def foo():
    iter = 0
    for i in range(10000000):
        iter += i
    return iter

start = time.time()
foo()
end = time.time()
duration = end - start
print("Time taken: ", duration)
"""
"""
Optimized Python code:

import time

def foo():
    iter = 0
    max_val = 10000000
    i = 0
    while i < max_val:
        iter += i
        i += 1
    return iter

start = time.time()
foo()
end = time.time()
duration = end - start
print("Time taken: ", duration)
"""