DEV Community 👩‍💻👨‍💻

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

Posted on

Do you even Refactor? 001

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

The Python code below takes about 12 seconds to execute. Refactor the getData function to make it run in less than 1 second. Post your answer in the comments.

import time

def getData():
  arr = []
  for i in range(1000*1000*100):
  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():

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

s9k96 profile image
Shubham Saxena

Skipping the loop

def getData():
    return [5]*1000*1000*100
Enter fullscreen mode Exit fullscreen mode
codr profile image
Ilya Nevolin Author


Does your company have something to share with DEV?

Create an Organization and start sharing content with the community on DEV.