logo

range() vs xrange() in Python

range() e xrange() sono due funzioni che potrebbero essere utilizzate per ripetere un certo numero di volte per cicli in Python. In Python3 , non esiste xrange, ma la funzione range si comporta come xrange in Python2. Se vuoi scrivere codice che verrà eseguito sia su Python2 che su Python3, dovresti usare range(). Entrambi sono implementati in modi diversi e hanno caratteristiche diverse ad essi associati. I punti di confronto sono:

  • Tipo di reso
  • Memoria
  • Utilizzo dell'operazione
  • Velocità

Funzione range() di Python

IL Funzione range() di Python restituisce una sequenza di numeri, in un determinato intervallo. L'uso più comune è quello di ripetere sequenze su una sequenza di numeri utilizzando i loop Python.



Funzione xrange() di Python

La funzione xrange() in Python viene utilizzata per generare una sequenza di numeri, simile alla funzione range() di Python. Python xrange() viene utilizzato solo in Python 2.x mentre la funzione range() in Python viene utilizzata in Python 3.x.

Restituisci Digita in range() vs xrange()

Questo xrange() la funzione restituisce il oggetto generatore che può essere utilizzato per visualizzare i numeri solo tramite loop. L'unico intervallo particolare viene visualizzato su richiesta e quindi richiamato valutazione pigra , mentre in Python la funzione range() restituisce un oggetto range (un tipo di iterabile).

Python3








# initializing a with range()> a>=> range>(>1>,>10000>)> # initializing a with xrange()> x>=> xrange>(>1>,>10000>)> # testing the type of a> print>(>'The return type of range() is : '>)> print>(>type>(a))> # testing the type of x> print>(>'The return type of xrange() is : '>)> print>(>type>(x))>

>

sql seleziona da più tabelle
>

Produzione:

The return type of range() is :   The return type of xrange() is :>

Velocità delle funzioni xrange() e range()

La variabile che memorizza il file allineare creato da intervallo() occupa più memoria rispetto alla variabile che memorizza l'intervallo utilizzando xrange(). La ragione fondamentale di ciò è che il tipo restituito di range() è elenco e xrange() è oggetto xrange().

Python3




import> sys> # initializing a with range()> a>=> range>(>1>,>10000>)> # initializing a with xrange()> x>=> xrange>(>1>,>10000>)> # testing the size of a> # range() takes more memory> print> (>'The size allotted using range() is : '>)> print> (sys.getsizeof(a))> # testing the size of x> # xrange() takes less memory> print> (>'The size allotted using xrange() is : '>)> print> (sys.getsizeof(x))>

linguaggio groosco
>

>

Produzione:

The size allotted using range() is :  80064 The size allotted using xrange() is :  40>

Operazioni Utilizzo delle funzioni xrange() e range()

Un range() restituisce l'elenco, tutte le operazioni che Potere essere applicato nell'elenco può essere utilizzato su di esso. D'altra parte, poiché xrange() restituisce l'oggetto xrange, le operazioni sono associate alla lista non può essere applicati a loro, quindi uno svantaggio.

Python3




# initializing a with range()> a>=> range>(>1>,>6>)> # initializing a with xrange()> x>=> xrange>(>1>,>6>)> # testing usage of slice operation on range()> # prints without error> print> (>'The list after slicing using range is : '>)> print> (a[>2>:>5>])> # testing usage of slice operation on xrange()> # raises error> print> (>'The list after slicing using xrange is : '>)> print> (x[>2>:>5>])>

>

>

Errore:

Traceback (most recent call last):  File '1f2d94c59aea6aed795b05a19e44474d.py', line 18, in   print (x[2:5]) TypeError: sequence index must be integer, not 'slice'>

Produzione:

The list after slicing using range is :  [3, 4, 5] The list after slicing using xrange is :>

Differenza tra range() e xrange() in Python

A causa del fatto che xrange() valuta solo l'oggetto generatore contenente solo i valori richiesti dalla valutazione pigra, quindi è Più veloce nell'implementazione rispetto a range().

intero Java

Punti importanti:

  • Se vuoi scrivere codice che verrà eseguito sia su Python 2 che su Python 3, usa range() poiché la funzione xrange è deprecata in Python 3.
  • range() è più veloce se si ripete la stessa sequenza più volte.
  • xrange() deve ricostruire l'oggetto intero ogni volta, ma range() avrà oggetti interi reali. (Tuttavia funzionerà sempre peggio in termini di memoria)

allineare()

xrange()

Restituisce un elenco di numeri interi. Restituisce un oggetto generatore.
La velocità di esecuzione è più lenta La velocità di esecuzione è più veloce.
Occupa più memoria poiché mantiene in memoria l'intero elenco di elementi. Occupa meno memoria poiché mantiene in memoria un solo elemento alla volta.
Tutte le operazioni aritmetiche possono essere eseguite poiché restituisce una lista. Tali operazioni non possono essere eseguite su xrange().
In Python 3, xrange() non è supportato. In Python 2, xrange() viene utilizzato per scorrere i cicli for.