import random
import time
TIMES = 1000
def add_remove_tuple( x) :
el = random .randint ( 0 , len ( x) -1 )
idx = x.index ( el)
x = x[ :idx] + x[ idx+1 :]
x = x + ( el, )
def add_remove_list( x) :
el = random .randint ( 0 , len ( x) -1 )
x.remove ( el)
#idx = x.index(el)
#x.pop(idx)
x.append ( el)
def time_tuple( ) :
x = tuple ( i for i in range ( 20000 ) )
t = time .time ( )
for i in range ( TIMES) :
add_remove_tuple( x)
return time .time ( ) - t
def time_list( ) :
x = [ i for i in range ( 20000 ) ]
t = time .time ( )
for i in range ( TIMES) :
add_remove_list( x)
return time .time ( ) - t
print ( "%d repetitions of add/remove at random position using tuple: %.3f seconds" % (
TIMES, time_tuple( ) ) )
print ( "%d repetitions of add/remove at random position using list: %.3f seconds" % (
TIMES, time_tuple( ) ) )
aW1wb3J0IHJhbmRvbQppbXBvcnQgdGltZQoKVElNRVMgPSAxMDAwCgpkZWYgYWRkX3JlbW92ZV90dXBsZSh4KToKCWVsID0gcmFuZG9tLnJhbmRpbnQoMCxsZW4oeCktMSkKCWlkeCA9IHguaW5kZXgoZWwpCgl4ID0geFs6aWR4XSArIHhbaWR4KzE6XQoJeCA9IHggKyAoZWwsKQoKZGVmIGFkZF9yZW1vdmVfbGlzdCh4KToKCWVsID0gcmFuZG9tLnJhbmRpbnQoMCxsZW4oeCktMSkKCXgucmVtb3ZlKGVsKQoJI2lkeCA9IHguaW5kZXgoZWwpCgkjeC5wb3AoaWR4KQoJeC5hcHBlbmQoZWwpCgpkZWYgdGltZV90dXBsZSgpOgoJeCA9IHR1cGxlKGkgZm9yIGkgaW4gcmFuZ2UoMjAwMDApKQoJdCA9IHRpbWUudGltZSgpCglmb3IgaSBpbiByYW5nZShUSU1FUyk6CgkJYWRkX3JlbW92ZV90dXBsZSh4KQoJcmV0dXJuIHRpbWUudGltZSgpIC0gdAoKZGVmIHRpbWVfbGlzdCgpOgoJeCA9IFtpIGZvciBpIGluIHJhbmdlKDIwMDAwKV0KCXQgPSB0aW1l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