Optimize the following Python code for improved performance.
def factorial(n):
 if n == 0:
   return 1
 else:
   return n * factorial(n - 1)
def factorial(n):
 fact = 1
 for i in range(1, n+1):
   fact *= i
  return fact