-1
I’m self-taught and I’m still learning. So I don’t know which formula would be better (consumes less hardware resource: cpu and memory)
Which one consumes the least in Python 3?
1
from random import randint
maior = menor = 0
for i in range(3):
n = randint(1, 100)
print(n)
if i == 0:
menor = n
maior = n
else:
if n < menor:
menor = n
if n > maior:
maior = n
print(maior, menor)
2
numeros[randint(1, 100), randint(1, 100), randint(1, 100)]
print(numeros)
print(min(numeros), max(numeros))
3
numeros = [randint(1, 100), randint(1, 100), randint(1, 100)]
print(numeros)
maior = (lambda n: max(n))(numeros)
menor = (lambda n: min(n))(numeros)
print(menor, maior)
It makes no difference, you’re using Python in a tiny problem. If you want less consumption do it in a problem that is worthwhile in a language that values it and gives ease to deal with that need.
– Maniero
Thank you for the answer. But it is a question of study even not of application. It is only a general doubt. But thanks anyway.
– Raden Vladi
Still, I need someone to help me figure out how much you use. I am 35 years old and I am studying Python 3 along with my nephew 10 years to encourage him to study programming and Python has a good learning curve for those who are starting.
– Raden Vladi
Now it makes even less sense to worry about it. To worry about the wrong things is to misdirect.
– Maniero
This type of optimization is not interesting. Optimizing code at this level only results in what is called legacy code. Python generates memory for you. Not only does that mean you won’t have to worry, it also means you can change without anyone asking you for your opinion. You can explore algorithmic complexity if you really want to understand complexity is computed in algorithms. Without wanting to leave it unanswered, you can use the module
dis
to parse python byte code and understand how python VM works. Worry about the code being readable above all.– Pedro Rodrigues
Thank you very much Pedro.
– Raden Vladi
It’s nice that you want to know this kind of thing, but not in this context that you want to apply. It turns out to be a loss of energy in something that doesn’t change anything (even if you were answered in detail, the real and practical use would be null and nothing guarantees that it will always be like this) and it has much more important things to worry about in relation to the chosen language. It’s basically what Maniero has said, don’t focus where you don’t have a problem in fact, especially at the stage you described being and because it’s a language that wasn’t meant for those who need this type of microtimization .
– Bacco
Thank you Bacco. So much so that I will keep the question and the answers that I found great, but I changed the tag only to Python 3x to not disturb other researches. Thank you to All!
– Raden Vladi
I think you can use time it to do tests
– Elton Nunes