难道一个类的对象成员的顺序对性能产生任何影响? [英] does order of members of objects of a class have any impact on performance?

查看:129
本文介绍了难道一个类的对象成员的顺序对性能产生任何影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一类莫名其妙的对象的二进制架构成员可下令对其中使用该类应用程序的性能产生影响?我想知道如何的情况下,决定荚成员秩序的答案是肯定的,因为程序员通过他们的declaraions的顺序定义成员的次序

May order of members in binary architecture of objects of a class somehow have an impact on performance of applications which use that class? and I'm wondering about how to decide order of members of PODs in case the answer is yes since programmer defines order of members via order of their declaraions

推荐答案

当然可以。 C ++保证内存中对象的顺序是一样的声明顺序,除非获得预选赛干预。

Absolutely. C++ guarantees that the order of objects in memory is the same as the order of declaration, unless an access qualifier intervenes.

对象,它们可直接相邻更可能是对同一高速缓存行,因此一个存储器访问将取他们两个(或两者从高速缓冲存储器清空)。高速缓存的有效性也可作为有用的数据的比例内它可以更高的改善。在code简单地说,空间位置转化为性能空间局部性。

Objects which are directly adjacent are more likely to be on the same cacheline, so one memory access will fetch them both (or flush both from the cache). Cache effectiveness may also be improved as the proportion of useful data inside it may be higher. Simply put, spatial locality in your code translates to spatial locality for performance.

此外,如杰里在评论中指出,为了可能影响填充量。排序通过减小尺寸,这也是通过减少对准的部件(通常处理的阵列作为一个元素的类型的,并且一个部件结构作为其最对齐部件)。不必要的填充可能会增加该结构的总尺寸,导致更高的存储器流量

Also, as Jerry notes in the comments, order may affect the amount of padding. Sort the members by decreasing size, which is also by decreasing alignment (usually treat an array as just one element of its type, and a member struct as its most-aligned member). Unnecessary padding may increase the total size of the structure, leading to higher memory traffic.

C ++ 03§9/ 12:

C++03 §9/12:

的非静态数据成员
  (非工会)类中声明没有
  其间访问符分配以至于后来成员
  类中的高地址
  目的。分配的顺序
  由分开的非静态数据成员
  访问说明符是不确定的
  (11.1)。实施调整
  要求可能会导致两个
  相邻构件不分配
  之后对方;所以可能
  对空间的要求,用于管理
  虚函数(10.3)和虚拟
  基类(10.1)。

Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

这篇关于难道一个类的对象成员的顺序对性能产生任何影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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