python - Why is .append() slower than setting the value in a pre-allocated array? -
python - Why is .append() slower than setting the value in a pre-allocated array? -
i'm trying speed part of code involves looping through , setting values in big 2d array. 1 of suggestions seek pre-allocating array rather using .append() pointed out in python .append() amortized o(1) operation.
however when tested using next code:
import time x = list() z = list() t1 = time.time() in range(10000): z.append([]) j in range(10000): z[i].append(0) t1 = time.time() in range(10000): x.append([]) j in range(10000): x[i].append(1) print(time.time()-t1) t1 = time.time() in range(10000): j in range(10000): z[i][j] = 1 print(time.time()-t1)
i consitently pre-allocated array taking 3-4 seconds less array isn't preallocated (~17s compared ~21). in code causing .append() based function take longer replacing value in pre-allocated array?
consider following:
from dis import dis def f1(): x = [] in range(10000): x.append([]) j in range(10000): x[i].append(0) homecoming x dis(f1) 2 0 build_list 0 3 store_fast 0 (x) 3 6 setup_loop 73 (to 82) 9 load_global 0 (range) 12 load_const 1 (10000) 15 call_function 1 18 get_iter >> 19 for_iter 59 (to 81) 22 store_fast 1 (i) 4 25 load_fast 0 (x) 28 load_attr 1 (append) 31 build_list 0 34 call_function 1 37 pop_top 5 38 setup_loop 37 (to 78) 41 load_global 0 (range) 44 load_const 1 (10000) 47 call_function 1 50 get_iter >> 51 for_iter 23 (to 77) 54 store_fast 2 (j) 6 57 load_fast 0 (x) 60 load_fast 1 (i) 63 binary_subscr 64 load_attr 1 (append) 67 load_const 2 (0) 70 call_function 1 73 pop_top 74 jump_absolute 51 >> 77 pop_block >> 78 jump_absolute 19 >> 81 pop_block 7 >> 82 load_fast 0 (x) 85 return_value
compared with:
def f2(): x = list() in range(10000): x.append([0]*10000) homecoming x dis(f2) 2 0 load_global 0 (list) 3 call_function 0 6 store_fast 0 (x) 3 9 setup_loop 40 (to 52) 12 load_global 1 (range) 15 load_const 1 (10000) 18 call_function 1 21 get_iter >> 22 for_iter 26 (to 51) 25 store_fast 1 (i) 4 28 load_fast 0 (x) 31 load_attr 2 (append) 34 load_const 2 (0) 37 build_list 1 40 load_const 1 (10000) 43 binary_multiply 44 call_function 1 47 pop_top 48 jump_absolute 22 >> 51 pop_block 5 >> 52 load_fast 0 (x) 55 return_value
how approach things can create huge difference.
python list time
Comments
Post a Comment