结构与类的性能 [英] Performances of Structs vs Classes

查看:121
本文介绍了结构与类的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有C ++中的类和C风格结构的性能比较与g ++ -O3选项。是否有任何基准或比较。我一直认为C ++类比结构更重,可能更慢(编译时对我来说不是很重要,运行时更重要)。我将实现一个B树,我应该实现它与类或结构的性能。

I wonder if there are performance comparisons of classes and C style structs in C++ with g++ -O3 option. Is there any benchmark or comparison about this. I've always thought C++ classes as heavier and possibly slower as well than the structs (compile time isn't very important for me, run time is more crucial). I'm going to implement a B-tree, should I implement it with classes or with structs for the sake of performance.

推荐答案

在运行时级别上,C ++中的结构体和类之间没有区别
因此,您的代码中是否使用 struct A A类

On runtime level there is no difference between structs and classes in C++ at all. So it doesn't make any performance difference whether you use struct A or class A in your code.

其他事情,使用一些功能,如构造函数,析构函数和虚函数,可能会有一些性能的惩罚(但如果你使用它们,你可能需要他们反正)。但你可以同样成功地在你的类或结构中使用它们。

Other thing, is using some features -- like, constructors, destructors and virtual functions, -- could have some performance penalties (but if you use them you probably need them anyway). But you can with equal success use them both inside your class or struct.

本文档您可以阅读关于C ++的其他性能相关细节。

In this document you can read about other performance-related subtleties of C++.

这篇关于结构与类的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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