Performance impact of realloc ()

前端 未结 4 2152
感情败类
感情败类 2021-02-18 15:41

I have a list of records, at the starting I dont know the number of records. I need to read them into array. so is it advisable to read all record one by one & doing reallo

4条回答
  •  温柔的废话
    2021-02-18 16:10

    It is a common technique to double the size of the array whenever it becomes full, as others have noted. In fact, using this technique ensures that no more than a constant amount of time is spent per element, as explained on WikiPedia.

    Depending on how fast the code needs to be and what source you are reading from, it might be a good idea to compute the size of the output in a separate pass. If you are reading from disk you should probably use a dynamic array but otherwise you should probably do whatever is easier to implement.

提交回复
热议问题