Do you even Refactor? 001
Ilja Nevolin

Ilja Nevolin @codr

About: Top Software Engineer x Founder

Location:
🌎
Joined:
Apr 28, 2020

Do you even Refactor? 001

Publish Date: Feb 13 '21
5 2

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):
    arr.append(5)
  return arr


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

@timed
def main():
  print(len(getData()))

main()
Enter fullscreen mode Exit fullscreen mode

Comments 2 total

  • Shubham Saxena
    Shubham SaxenaFeb 13, 2021

    Skipping the loop

    def getData():
        return [5]*1000*1000*100
    
    Enter fullscreen mode Exit fullscreen mode
Add comment