deepcopy and python - tips to avoid using it?

前端 未结 2 1899
感动是毒
感动是毒 2020-12-23 22:39

I have a very simple python routine that involves cycling through a list of roughly 20,000 latitude,longitude coordinates and calculating the distance of each point to a ref

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 23:16

    I understand this doesn't directly address your question (and I know this is an old question), but since there is some discussion about performance it may be worthwhile to look at the append operation. You may want to consider "pre-allocating" the array. For example:

    array = [None] * num_elements
    for i in range(num_elements):
        array[i] = True
    

    versus:

    array = []
    for i in range(num_elements):
        array.append(True)
    

    A simple timeit run of these two methods shows a 25% improvement if you pre-allocate the array for even moderate values of num_elements.

提交回复
热议问题