Skip to content

Instantly share code, notes, and snippets.

@Omar-Gonzalez
Last active July 30, 2018 14:41
Show Gist options
  • Save Omar-Gonzalez/c77ba9fb80db575f017cdcd6189ac7b3 to your computer and use it in GitHub Desktop.
Save Omar-Gonzalez/c77ba9fb80db575f017cdcd6189ac7b3 to your computer and use it in GitHub Desktop.
'''
Avoid code duplication for functions
1) solve duplcation
2) Avoid mix function main logic porpuse
'''
import time
'''
Decorator function:
'''
def time_it(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs) # function to be decorated execution here
end = time.time()
print(func.__name__ + ' took ' + str((end - start) * 1000) + ' mil seconds')
return result
return wrapper
'''
Calc square area is the main porpuse of this function
'''
@time_it
def calc_square(numbers):
result = []
for number in numbers:
result.append(number * number)
return result
'''
Calc cube volume is the main purpose of this function
'''
@time_it
def calc_cube(numbers):
result = []
for number in numbers:
result.append(number * number * number)
return result
array = range(1, 100000)
out_square = calc_square(array)
out_cube = calc_cube(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment