与小于16字节为什么结构体更好 [英] Why is struct better with being less than 16 bytes

查看:220
本文介绍了与小于16字节为什么结构体更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学结构;有些书建议创建结构,如果它有一个实例大小小于 16个字节

I'm studying struct; some book advises creating struct if it has an instance size less than 16 bytes.

为什么?

感谢您的任何答复。

推荐答案

这是因为16个字节就是编译器开始使用一个或两个简单的移动指令复制结构的内存块,而不是门槛。

It's because 16 bytes is the threshold where the compiler starts copying structs as block of memory instead of using one or two simple move instructions.

编译器优化结构的复制,当他们是小。一个结构是例如八个字节可以被复制为一个单一的64位值。一个结构即16个字节可以是副本一个或两个奇异值(依赖于处理器架构)。当结构大于16字节时,编译器不会尝试优化移动更多,而回退是调用一个方法,将内存块。

The compiler optimises the copying of structures when they are small. A struct that is for example eight bytes can be copied as a single 64 bit value. A struct that is 16 bytes can be copies as one or two singular values (depending on the processor architecture). When the structure is larger than 16 bytes, the compiler doesn't try to optimise the move any more, and the fallback is to call a method that copies a block of memory.

(注:16个字节的门槛可能会有所不同编译器的版本,它似乎它实际上试图优化以后在新版本这一点,但优化的code将仍然是一个很大的移动指令,相比于复制引用一个对象,它仍然是一个移动操作。)

(Note: the threshold of 16 bytes may differ depending on the version of compiler, it seems as it actually tries to optimise beyond that point in newer versions, but the optimised code will still be a lot of move instructions, compared to copying a reference to an object which still is a single move operation.)

编辑:
下面是一个测试,我做了我的64位系统的复制结构半个十亿次的结果是:


Here is the result of a test that I did on my 64 bit system copying structs half a billion times:

struct 4    : 272 ms.
struct 8    : 235 ms.
struct 16   : 317 ms.
struct 32   : 625 ms.
struct 64   : 1280 ms.
struct 128  : 4659 ms.
struct 256  : 8020 ms.

正如所看到的,下面16个字节的时间是不是线性的,尽管16个字节是四倍之多的4个字节,它并不需要四倍的时间。上述16个字节的时间是线性的,所以倍增尺寸两倍的时间。这就是它会开始使用多个移动。以上64个字节有一个跳跃,在那里的时候突然四倍当尺寸双打。这就是回退会开始被使用。

As you see, below 16 bytes the time is not linear, although 16 bytes is four times as much as 4 bytes, it doesn't take four times longer. Above 16 bytes the time is linear, so doubling the size double the time. That's where it would start using multiple moves. Above 64 bytes there is a jump, where the time suddenly quadruples when the size doubles. That's where the fallback would start to be used.

这篇关于与小于16字节为什么结构体更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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