在C循环中对malloc()的调用会影响性能吗?

我有一个要求,我必须通过处理给定的动态数组来创建一个新的数组。我不知道我的新数组大小是多少。但最大大小是旧数组的2倍。

背景: 在函数内部,它应该遍历数组中的每个元素,并根据数量将一个或两个元素添加/删除到新数组中。

实现方式: 我如何实现上述要求的方法是,创建一个链表并根据检查条件添加一个或两个新节点。

伪代码:

void addNode(node_t** head, double newVal, log_error_message message) {
     /* Create new node to insert */
    node_t *node = calloc (1, sizeof *node);
    if (node == NULL) {
        message("error: virtual memory exhausted.\n");
        exit (2);
    }
    node->val = newVal;
    node->next = NULL;

    /* Insert node at end */
    if (!(*head))               /* handle first node */
        *head = node;
    else {                      /* handle remaining  */
        node_t *cur = *head;
        while (cur->next)
            cur = cur->next;
        cur->next = node;
    }
}

void create_array_from_list(double** data, node_t* head, log_error_message message) {
  int size = 1;
  node_t* current;
    int count = 0;
    double *value_tmp;
  double *value = malloc(sizeof(double) * size);
  if (value == NULL){
      message("Error: Failed to allocate memory for value.");
      exit(1);
  }
  memset(value,0,sizeof(double)*size);

  current = head;

  while (current != NULL) {
    value[count] = current->val;
    if (count+1 == size) {
      /* need more space */
      size += 10;
      value_tmp = (double *)realloc(value, sizeof(double)*size);
      if (value_tmp == NULL) {
        message("Fatal error -- out of memory!");
        exit(1);
      }
      value = value_tmp;
    }
    count = count+1;
    current = current->next;
  }
  *data = value;
}

int function(double* old_array, int size){
    //declarations 
    node_t* node;
    double* data;

     for(i=0;i<size;i++){
          if(<condition to check>){
               addNode(&node, val);//It involves malloc function call to create new node
          } else if(second cond){
                addNode(&node,val);
                addNode(&node,val);
          } else {
                delLastnode(&node);//Involves free()
          }
      create_array_from_list(&data,node);
       freeList(node);
}

对于一些小例子,它工作得很好。但是,当将具有50万个值的数组作为该函数的输入传递时,性能将急剧下降。

是否有解决上述问题的替代方法,或者是否需要进行其他修改以提高性能?

谢谢