Skip to content

Conversation

Darakuu
Copy link

@Darakuu Darakuu commented Oct 30, 2023

No description provided.

]
MIN_NUMBER = 1
MAX_NUMBER = 8
if weekDay not in range(MIN_NUMBER, MAX_NUMBER): # range usa un intervallo [a,b[
Copy link
Collaborator

@TendTo TendTo Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questa sintassi e' estremamente espressiva, ma devo fare l'avvocato del diavolo e farti notare che e' una operazione estremamente inefficiente, in quanto stai iterando sopra un generatore fino a trovare il valore scelto (o fino alla fine, se non presente). Se i valori fossero molto pi' grandi sarebbe uno spreco enorme.
Una alternativa semplice ma a complessita' costante sarebbe:

if not MIN_NUMBER <= weekDay < MAX_NUMBER: # Se inverti il corpo di if ed else puoi anche rimuovere il not

Giusto per tua curiosita', puoi provare ad eseguire questo script e vedere tu stesso:

from timeit import default_timer as timer

MIN_NUMBER = 1
MAX_NUMBER = 100000000000000000000000000000000000000000000
weekDay = -1

start = timer()
if weekDay not in range(MIN_NUMBER, MAX_NUMBER):
    print("Error: Invalid day of the week")
end = timer()
print(end - start) # 2.203899905201979e-05

start = timer()
if not MIN_NUMBER <= weekDay < MAX_NUMBER:
    print("Error: Invalid day of the week")
end = timer()
print(end - start) # 2.132998583372682e-06 (un'ordine di grandezza piu' veloce!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants