为什么new()/delete()比malloc()/free()慢? [英] Why are new()/delete() slower than malloc()/free()?

查看:156
本文介绍了为什么new()/delete()比malloc()/free()慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么new()/delete()比malloc()/free()慢?

Why new()/delete() is slower than malloc()/free()?

感谢您到目前为止的回答.请感谢new()和delete()的标准C ++实现规范,如果有的话,谢谢!

Thanks for the answers so far. Please kindly point out specifications of standard C++ implementation of new() and delete() if you have them thanks!

推荐答案

看一下这段C代码:

struct data* pd = malloc(sizeof(struct data));
init_data(pd);

C ++中的new运算符实际上是在执行上面的代码.这就是为什么它比malloc()慢的原因.

The new operator in C++ is essentially doing what the above piece of code does. That's why it is slower than malloc().

delete类似.它等效于此:

deinit_data(pd);
free(pd);

如果构造函数和析构函数为空(例如内置函数),则newdelete的速度不应慢于malloc()free()的速度. (如果它们是 ,通常是由于常见的实现在后台调用了malloc()/free(),因此它们是它们的包装.包装成本.另外,可能会有代码需要找出不需要调用任何构造函数/析构函数的方法.这也需要花费费用.)

If the constructors and destructors are empty (like for built-ins), new and delete shouldn't be slower than malloc() and free() are. (If they are, it's often due to the fact that common implementations call malloc()/free() under the hood, so they are a wrapper around them. Wrapping costs. Also, there might be code that needs to find out that no constructors/destructors are to be called. That would cost, too.)

编辑:要回答您的其他问题:

Edit To answer your additional question:

newdelete不是函数,它们是运算符.这:new data()被称为新表达式.它有两件事.首先,它调用operator new,然后通常通过调用适当的构造函数来初始化对象. (我之所以说通常",是因为内置程序没有构造函数.但是,包含内置程序的新表达式仍然可以实现同样的功能.)

new and delete aren't functions, they are operators. This: new data() is called a new expression. It does two things. First it calls the operator new, then it initializes the object, usually by invoking the appropriate constructor. (I say "usually" because built-ins don't have constructors. But a new expression involving a built-in works the same nevertheless.)

您可以操纵这两个阶段.您可以创建自己的构造函数来操纵类型的初始化,还可以重载operator new(即使有几个具有不同附加参数的重载,如果需要,也可以专门针对每个类),以操纵空闲存储的分配.如果您没有实现自己的operator new,则使用标准库中的版本.此的常见实现称为malloc().

You can manipulate both of these phases. You can create your own constructors to manipulate initialization of your types and you can overload operator new (even with several overloads having different additional arguments and also specifically for each class, if you want) in order to manipulate allocation of free storage. If you don't implement your own operator new, the version from the standard library is used. A common implementation of this calls malloc().

同样,如果您编写delete pd(称为 delete表达式),则会发生两件事:根据pd,对象通常会通过调用其析构函数进行初始化,然后是内存通过调用相应的operator delete释放.

Likewise, if you write delete pd, called a delete expression, two things happen: depending on pd, the object is de-initialized, usually by calling its destructor, then the memory is released by calling the appropriate operator delete.

同样,您可以通过编写自己的析构函数和编写自己的operator delete版本来操纵两个阶段. (标准库随附的operator delete版本通常实现为调用free().)

Again, you can manipulate both phase, by writing your own destructor, and by writing your own version of operator delete. (The version of operator delete that comes with your standard library is often implemented to call free().)

这篇关于为什么new()/delete()比malloc()/free()慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆