More efficient code, but slower program

Dennis Lee Bieber wlfraed at ix.netcom.com
Wed Jul 27 12:35:23 EDT 2022


On Wed, 27 Jul 2022 14:58:02 +0200, Cecil Westerhof <Cecil at decebal.nl>
declaimed the following:

>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
>
	Were these done in the same program/session? If so, the first
invocation may be initializing/caching the first 100 integers (Python tends
to keep some number of integers in a permanent cache to speed later access
to common values).

	Also rather than * unpacking of the range iterator into a [] list...
just...

>>> list(range(100))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99]
>>> 

... will do it. 

	Also, if your goal is to /remove/ and entry from the list via some
index, you might consider if this is more effective than copying the list
and THEN popping a value.

>>> full = list(range(100))
>>> import random
>>> idx = random.randint(0, len(full))
>>> idx
74
>>> trim = full[:idx] + full[idx+1:]
>>> trim
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78,
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99]
>>> trim == full
False
>>> 

or

>>> 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,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78,
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99]
>>> 

	The first does two partial copies skipping the item to be removed, and
joins the results into a new list. The second does a full copy and DELETES
the element to be removed from the copy.

>>> trim3 = full[:]
>>> trim3.remove(idx)
>>> trim3
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78,
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99]
>>> 

	This option works because the list is sequential integers and the index
matches the values in the list (.remove() removes the first MATCHING
element, so if the list can have duplicates is may not remove the one AT
the index position).



-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/


More information about the Python-list mailing list