Resources required per formula

Asked

Viewed 44 times

-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)
  • 2

    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.

  • 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.

  • 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.

  • 2

    Now it makes even less sense to worry about it. To worry about the wrong things is to misdirect.

  • 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.

  • Thank you very much Pedro.

  • 1

    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 .

  • 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!

  • I think you can use time it to do tests

Show 4 more comments

1 answer

0

The consumption is minimal in its functions, but if you really want to know would advise to use the module timeit , you will have the run time of each function I believe that consequently the one that takes less time is the one that requires less processing resources.

from random import randint
import timeit
def maior_menor():

    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)
def menor_maior():
     numeros[randint(1, 100), randint(1, 100), randint(1, 100)]
     print(numeros)
     print(min(numeros), max(numeros))
t = timeit.Timer("maior_menor(1,1000from __main__ import maior_menor")
print t.repeat()
t = timeit.Timer("menor_maior(1,1000from __main__ import menor_maior")
print t.repeat()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.