Which is faster: Stack allocation or Heap allocation

前端 未结 23 2048
悲&欢浪女
悲&欢浪女 2020-11-22 07:40

This question may sound fairly elementary, but this is a debate I had with another developer I work with.

I was taking care to stack allocate things where I could, i

23条回答
  •  日久生厌
    2020-11-22 07:52

    Stack allocation is much faster since all it really does is move the stack pointer. Using memory pools, you can get comparable performance out of heap allocation, but that comes with a slight added complexity and its own headaches.

    Also, stack vs. heap is not only a performance consideration; it also tells you a lot about the expected lifetime of objects.

提交回复
热议问题