-
[python] '리스트 공간 확보 후 원소 대입'과 'list.append를 이용한 원소 삽입' 간 수행 시간 차이아무거나적어~ 2023. 7. 29. 21:08
[0, 1, 2, 3, ...] 와 같은 리스트를 생성해야 할 때,
전체 원소 크기만큼 리스트 공간을 확보 후 원소들을 대입하도록 하자
-> 내부적으로 list가 dynamic array 자료구조(자료구조가 초기에 할당한 배열의 capacity를 넘어설 때 더 큰 배열로 원소들을 옮겨야 됨)이기 때문에 append를 이용한 연산이 더 시간이 많이 걸리는 것으로 보인다123456789101112131415161718192021N = 10*6def a(N):ret = [0]*Nfor i in range(N):ret[i] = ireturn retdef b(N):ret = []for i in range(N):ret.append(i)return retif __name__ == '__main__':from timeit import timeitprint(timeit('a(N)', number = 1000, globals = globals()))How is Python's List Implemented?
Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn't good enough to look at the source code.
stackoverflow.com
'아무거나적어~' 카테고리의 다른 글
[deeplearning] sigmoid의 종류 (0) 2023.07.31 [python] np.random.normal 이 왜 정규분포에서 샘플한거임?? (0) 2023.07.30 [jupyter] !, % command 차이 (0) 2023.07.29 [python] timeit 모듈을 사용한 numpy 배열 제곱 비교 (0) 2023.07.29 [python] timeit 모듈을 사용한 리스트 생성 수행시간 비교 (0) 2023.07.29