【问题标题】:Are calloc/malloc faster than operator new in C++calloc/malloc 比 C++ 中的 operator new 快吗
【发布时间】:2014-06-28 18:18:27
【问题描述】:

我想知道的是,如果我在 c++ 程序中使用 calloc/malloc 而不是 operator new,它是否会使内存分配更快,或者在使用 c++ 编译器编译程序时几乎没有关系。

编辑:

我想应该很明显我没有使用 new 运算符来调用构造函数。只是像数组一样分配内存。

【问题讨论】:

  • 询问x是否比y快的问题大多应该用benchmark it!回答
  • 在 C++ 的大多数上下文中,malloc 是不合适的。所以快不快并不重要。
  • 您需要拼写为operator new 以避免所有关于苹果和橙子的反应。
  • 也许您应该将调用类型的示例发布到 malloccallocoperator new 或您尝试比较的 new 运算符。
  • 如果你说出来就很明显了。事实上,假设任何这样的事情都是没有根据的猜测。不清楚你到底在问什么。

标签: c++ dynamic-memory-allocation


【解决方案1】:

对于我测试过的大多数编译器,使用 new 时执行的额外初始化意味着它分钟malloc 慢(至少在处理两者的简单类型时)至少有模糊的可比性)。例如:

Test Name:   D000001                         Class Name:  Allocation
CPU Time:        56.8  nanoseconds           plus or minus       2.84
Wall/CPU:        1.02  ratio.                Iteration Count:  419430400
Test Description:
 Dynamic array allocation, use and deallocation time measurement
 Dynamic array of 1000 integers
 get space on heap using malloc() and use it in a procedure on each call



Test Name:   D000002                         Class Name:  Allocation
CPU Time:         238  nanoseconds           plus or minus       11.9
Wall/CPU:        1.03  ratio.                Iteration Count:  104857600
Test Description:
 Dynamic array allocation, initialization, use and deallocation time measurement

 Dynamic array of 1000 integers
 get space on heap using malloc() and use it in a procedure on each call



Test Name:   D000003                         Class Name:  Allocation
CPU Time:        60.4  nanoseconds           plus or minus       3.02
Wall/CPU:        1.02  ratio.                Iteration Count:  419430400
Test Description:
 Dynamic array allocation, use and deallocation time measurement
 Dynamic array of 1000 integers
 get space on heap using NEW and use it in a procedure on each call



Test Name:   D000004                         Class Name:  Allocation
CPU Time:         249  nanoseconds           plus or minus       12.4
Wall/CPU:        1.03  ratio.                Iteration Count:  104857600
Test Description:
 Dynamic array allocation, initialization, use and deallocation time measurement

 Dynamic array of 1000 integers
 get space on heap using NEW and use it in a procedure on each call

因此,malloc 平均速度更快,但速度有足够的变化(newmalloc),new 的单独调用实际上可能比 malloc 的单独调用更快.

【讨论】:

  • 感谢您进行这些测试
  • 嘿,杰瑞,很好的答案。不过需要注意的是,这些测量仅适用于经过测试的编译器(或多个编译器)。
  • @wich:理论上,你是绝对正确的。然而,事实上,在相当多的编译器上进行的测试表明,上述趋势已经足够普遍,这是一个合理的假设,直到或除非你有理由不相信。
  • @JerryCoffin 是的,我绝对希望大多数主流编译器对 mallocoperator new 的处理大致相同。
【解决方案2】:

你在比较苹果和橘子。 malloc()calloc() 分配内存。 new 通过可能被覆盖的运算符分配内存,并调用构造函数。它们做不同的事情。比较它们是无效的。 “正在使用 C++ 编译器来编译程序”这一事实是 (a) 显而易见且 (b) 无关的。

【讨论】:

  • 然后考虑它们等价的情况,即对于 malloc 构造函数什么都不做的情况,或者对于 calloc 零初始化的情况。或者将它们与对 operator new 函数的调用进行比较。或者最好将问题作为重复项关闭...
  • @EJP 我想很明显在这种情况下我不会使用 new 运算符来调用构造函数。
  • @adnankamili 一点也不明显。
  • @MarcGlisse 如果您对我的回答发表评论,您将引入问题中未说明的其他约束。如果您对问题发表评论,则说明您在错误的地方。
  • @EJP 我在评论你的回答,所以让我扩展一下:说这个问题没有意义,因为在某些情况下它似乎并不像确定它有的情况那样有效一个意义并专注于那些。此外,由于您有足够的代表,您应该关闭问题(作为重复),而不是发布 3 行答案。 PS:我没有投反对票。
【解决方案3】:

malloc 在 c++ 中执行operator new等效 任务,而不是new operator。他们只是分配一个足够大的内存位置以满足您的需要。 new operator 通过在内存中调用constructor 创建一个object,另外正确 数据填充分配的内存。 calloczeroes 填充位。

malloc/callocoperator new 中的哪一个效率更高?这取决于实现。两者都将一定大小的返回内存分配为void*

【讨论】:

    【解决方案4】:

    new 通常被实现为只调用malloc 的包装器。不同之处在于它添加的 C++ 语义(调用构造函数、处理异常等)。

    【讨论】:

      【解决方案5】:

      与任何有关 C++ 的性能问题一样,它取决于您的编译器。但是任何广泛使用的编译器都会将new 优化为与malloc 一样快。编写一个测试程序,自己看看。

      (我假设您的意思是new char[100],而不是构造一个对象)

      【讨论】:

        猜你喜欢
        • 2017-05-06
        • 1970-01-01
        • 2017-09-08
        • 2013-11-17
        • 2016-04-21
        • 2011-09-28
        • 2012-03-02
        相关资源
        最近更新 更多