DEV Community 👩‍💻👨‍💻

Cover image for Do you even Refactor? 002
Ilya Nevolin
Ilya Nevolin

Posted on

Do you even Refactor? 002

Code refactoring is crucial but often overlooked. It can improve the design and performance of existing code.

The Python code below takes about 17 seconds to complete. Refactor the getData function to make it run in less than 5 seconds. Post your answer in the comments.

import time

def getData():
  arr = [5] * 1000 * 1000 * 100
  for i in range(1, len(arr)):
    if i % 5 == 0:
      arr[i] = 0
  return arr

def timed(func):
  def run():
    Tstart = time.time()
    Tend = time.time()
    Tdt = round(Tend - Tstart, 2)
    print(Tdt, 'seconds')
  return run

def main():
  data = getData()
  print('len:', len(data), 'sum:', sum(data))

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Hacktoberfest is happening now!

It is a month-long celebration of open source. For a lot of devs, its their introduction to open source.

Check out the Hacktoberfest tag on DEV to keep up with the latest!