Im works filteron interable and wants to keep the result in a sequence (I need a sequence so I can use random.choiceon it). I noticed that creating a collection from a filter object is much faster than creating a list or tuple. Why is this? First, I want the filter type to be a subtype of set, which would explain this, but the function filteris actually identical to the expression of the generator, so it cannot be truly internal.
I checked the following test to check the speed:
import time
def test ( n, seq ):
for method in ( set, list, tuple ):
t = time.time()
for i in range( n ):
method( seq )
print( method.__name__, ( time.time() - t ) )
someFilter = filter( lambda x: x % 3 == 0, range( 1000 ) )
test( 10000000, someFilter )
And the results clearly talked about using the kit:
set 1.9240000247955322
list 8.82200002670288
tuple 7.031999826431274
, ? , , ? - ?
, range set list tuple ( ).
:
Svens , , :
import time
def testFilter ( n, test, rangeSize ):
for method in ( set, list, tuple ):
t = time.time()
for i in range( n ):
method( filter( test, range( rangeSize ) ) )
print( method.__name__, ( time.time() - t ) )
testFilter( 100000, lambda x: x % 3 == 0, 1000 )
, , list tuple , set isnt , , :
set 27.868000030517578
list 27.131999969482422
tuple 27.138000011444092