When you Over Optimize a Python Function

preview_player
Показать описание
#coding and #programming with #python
Рекомендации по теме
Комментарии
Автор

If you use the 🤮 88 times instead of the 💩 721 times, it pipes the code through the flux capacitor and gives you your answer the day before you run it!

Tenly
Автор

They had us in the first half, not gonna lie

danishhaider
Автор

# I'm learning Python, so I took it as a homework assignment
def poo_721(func):
def wrapper(*args, **kwargs):
print("💩" * 721)
return func(*args, **kwargs)
return wrapper

@poo_721
def calculate_grade(score):
if score < 60: return "F"
if score > 89: return "A"
return chr(74 - score // 10)

# driver code
for n in range(101):
print(n, calculate_grade(n))

# EDIT: I implemented a poo decorator. The idea being that the poo decorator can be used to optimize many other functions :)

humanrightsadvocate
Автор

I mean, putting a random delay is a legit thing.

Customer: build this script!
Me: *builds super fast script*
Customer: When I input a number, the script produces the result instantly. It feels like it doesn't even think about the result. Are you sure it works correctly?
Me: *inserts 1s delay*
Customer: Ahhh, much better! Now I know it's calculating because it takes a bit of time

johantaube
Автор

Just tested in production. Blazingly fast.

lorisdeluca
Автор

C++ & C: "We are fast!!!!"
Meanwhile Python: "Let's add sleep() to make sure this function doesn't run too fast."

rotteegher
Автор

Imagine your coworker asking on the team group chat why are there 721 poop emojis in the server logs

panlis
Автор

If we aren’t using quantum optimisations, most efficient way is by making a dict that contains the number and grade. You can make separate function for doing distance calculation with NUMPY.

RemoteAccessGG
Автор

If you had just said “optimize this function by making it slower” and added a sleep for 5ms, I’m sure some people would be routinely doing it now.

jakobullmann
Автор

you can do it in 1 line of code
def f(score):
return 'F' if score < 60 else chr((100 - score) // 10 + ord('A'))

mhdimhdi
Автор

"the sleep ensures the function isn't run too fast"

Don't worry, by writing your code in python, it will never run too fast.

marklonergan
Автор

I am a complete noob but this guy explained so well I understood everything

akshayrathod
Автор

I used this trick. My cto told me I get a raise!! Thanks man!!

CreatorsExpress
Автор

Thanks I’ll definitely use these tricks on the code that my boss told me to improve. Can’t wait to see his satisfaction tomorrow ! :)

EvilCherry
Автор

Never thought I'd see a Python comedian.

OneWeirdDude
Автор

Rob Mulla has been and is still such a help throughout my Data Science journey that for a sec or two I considered even this advice legit.

kaykayw
Автор

This guy taught me the basics of data science in Python. This should be true.

mrzorak
Автор

It seems very accurate according to some coders of legacy codes I have faced.

SchvarczFranco
Автор

I did put it straight into production code. Thx for the advice, bro.

michaelmueller
Автор

You can also use pandas to train a ML model then use the continuem transfunctioner to egg out 10% more speed.

D_To_The_J