values = [*range(100)]
Cecil Westerhof <Cecil@decebal.nl> writes:
values = [*range(100)]
In many cases, any iterable is just fine and a list is not
required, just as peudo-random numbers often are just fine and
real-world entropy is not required.
Usually one wants to write code for readability, and thinking
too much about runtime efficiency optimizations is in vain,
because one might get different results with a different
version of Python or on a different machine.
It is not very important, but I am just curious.
Original I had in a program:
values = [*range(100)]
But because it is done quite often I expected that initialising:
range_list = [*range(100)]
and then use:
values = range_list.copy()
Would be more efficient. So I tried:
timeit('values = [*range(100)]')
1.6964535564184189
and:
timeit('new_values = values.copy()', 'values = [*range(100)]')
0.6457642465829849
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,list(range(100))
74full = list(range(100))
import random
idx = random.randint(0, len(full))
idx
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,trim = full[:idx] + full[idx+1:]
trim
Falsetrim == full
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,trim2 = full[:]
del trim2[idx]
trim
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,trim3 = full[:]
trim3.remove(idx)
trim3
ram@zedat.fu-berlin.de (Stefan Ram) writes:
Cecil Westerhof <Cecil@decebal.nl> writes:
values = [*range(100)]
In many cases, any iterable is just fine and a list is not
required, just as peudo-random numbers often are just fine and
real-world entropy is not required.
In this case both are. I must select (several times) a random element
from the list. So I need the list.
I also want the randomness to be as good as possible to make the
'simulation' as good as possible.
On 2022-07-27 at 17:48:47 +0200,
Regarding "Re: More efficient code, but slower program,"
Cecil Westerhof via Python-list <python-list@python.org> wrote:
ram@zedat.fu-berlin.de (Stefan Ram) writes:
Cecil Westerhof <Cecil@decebal.nl> writes:
values = [*range(100)]
In many cases, any iterable is just fine and a list is not
required, just as peudo-random numbers often are just fine and
real-world entropy is not required.
In this case both are. I must select (several times) a random element
from the list. So I need the list.
I also want the randomness to be as good as possible to make the
'simulation' as good as possible.
"[A]s good as possible" for simulations and tests may not require the cryptographic quality numbers from SystemRandom. Many/most pseudo
random number generators are optimized for statistically normalized
outputs, and are repeatable as a bonus (again, often a requirement for certain types of simulations; YMMV).
Also, what if you shuffled the list first (e.g., with random.shuffle)
and then iterated through it directly? Repeatedly removing arbitrary elements from the middle of a list is potentially expensive.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (2 / 14) |
Uptime: | 241:52:10 |
Calls: | 6,624 |
Files: | 12,175 |
Messages: | 5,320,145 |