且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

访问4字节长的单个字节(优化)

更新时间:2023-11-14 09:43:22

*******@gmail.com 写道:
an*******@gmail.com wrote:

在*给定架构的机器上*(就字节序等而言),我希望
能够以

的速度访问长(*一次性*)的单个字节。


版本A,版本B或版本C更好吗?
On a machine of *given architecture* (in terms of endianness etc.), I
want to access the individual bytes of a long (*once-off*) as fast as
possible.

Is version A, version B, or version C better?



测量它们并查找。

Measure them and find out.


现在如果需要访问单个字节*整个时间*?

A2,B2,C2或D2更快?
Now what if one needs to access the individual bytes the *whole time*?
Is A2, B2, C2 or D2 faster?



测量它们并找出答案。


-

Chris"表现一无所获没有测量 Dollin


Hewlett-Packard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597英格兰

Measure them and find out.

--
Chris "performance is nothing without measurement" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


an*******@gmail.com 写道:
an*******@gmail.com wrote:

在*给定架构的机器上*(就字节序等而言),我想要访问一个长的单个字节(*一次性*)和

一样快。


版本A,版本B或版本C更好吗?
On a machine of *given architecture* (in terms of endianness etc.), I
want to access the individual bytes of a long (*once-off*) as fast as
possible.

Is version A, version B, or version C better?



Mu。


微优化的规则之一:

不要做它。

规则二的微优化(仅限专家!):

不要这样做。

规则三微优化(仅在胁迫下):

测量,测量,测量。


除非你知道它重要,否则假设它不是,并写下

最清晰的代码。如果您认为您确实知道这很重要,请先收集

证据。只有通过在优化设置下,在您的

项目中使用您的实现,您才能最快地测量哪个是最快的?并且不要感到惊讶

发现你错了,差价不超过0.5%,

,误差为1%。


Richard

Mu.

Rule one of micro-optimisation:
Don''t Do It.
Rule two of micro-optimisation (for experts only!):
Don''t Do It Yet.
Rule three of micro-optimisation (only under duress):
Measure, Measure, Measure.

Unless you _know_ that it matters, assume that it doesn''t, and write the
clearest code. If you think you do know that it matters, first gather
evidence. Only by measuring which is the fastest will you know which is
the fastest - on your machine, using your implementation, in your
project, under your optimisation settings. And don''t be surprised to
find out that you were wrong, and the difference is no more than 0.5%,
with an error of 1%.

Richard


** *****@gmail.com 写道:
an*******@gmail.com writes:

嗨!


在*的机器上鉴于架构*(就字节序等而言),我希望
能够以

的速度访问长(*一次性*)的单个字节。


版本A,版本B或版本C更好吗?还有其他

替代品吗?


/ ****版本A ****** /

{

long mylong = -1;


printf(" 0x%02x 0x%02x 0x%02x 0x%02x \ n",\

(unsigned char)mylong,\

(unsigned char)(mylong> 8),\

(unsigned char)(mylong> > 16),\

(unsigned char)(mylong>> 24));

}


/ ****版本B ****** /

{

long mylong = -1;

unsigned char f_b [4 ];


*((长*)& f_b)= mylong;


printf(" 0x%02x 0x%02x 0x %02x 0x%02x \ n",f_b [0],f_b [1],f_b [2],

f_b [3]);

}


/ ****版本C ****** /

{

union align_array_and_long {

unsigned char four_b [4];

long dummy;

};


long mylong = -1;

union alig n_ar​​ray_and_long四;


four =(union align_array_and_long)mylong;


printf(" 0x%02x 0x%02x 0x%02x 0x% 02x \ n",\

four.four_b [0],\

four.four_b [1],\

four.four_b [2],\

four.four_b [3]);

}


我的感觉是版本C是***的。
Hi!

On a machine of *given architecture* (in terms of endianness etc.), I
want to access the individual bytes of a long (*once-off*) as fast as
possible.

Is version A, version B, or version C better? Are there other
alternatives?

/**** Version A ******/
{
long mylong = -1;

printf("0x%02x 0x%02x 0x%02x 0x%02x\n", \
(unsigned char) mylong , \
(unsigned char) (mylong >8), \
(unsigned char) (mylong >>16), \
(unsigned char) (mylong >>24));
}

/**** Version B ******/
{
long mylong = -1;
unsigned char f_b[4];

*((long *)&f_b) = mylong;

printf("0x%02x 0x%02x 0x%02x 0x%02x\n", f_b[0], f_b[1], f_b[2],
f_b[3]);
}

/**** Version C ******/
{
union align_array_and_long {
unsigned char four_b[4];
long dummy;
};

long mylong = -1;
union align_array_and_long four;

four = (union align_array_and_long) mylong;

printf("0x%02x 0x%02x 0x%02x 0x%02x\n", \
four.four_b[0], \
four.four_b[1], \
four.four_b[2], \
four.four_b[3]);
}
My feeling is the Version C is best.



为了最快,请尝试:


printf(" 0x%08lx \ n",mylong); / * :-) * /


版本B和C,调用未定义的行为。定义的方式

版本B是:


void * vp =& mylong;

unsigned char * cp = vp;

/ *现在做你想要的cp [0]到cp [sizeof long] * /


没有必要撒谎数组。版本C非常可靠,但是该标准不保证访问任何

工会会员而不是最后一个工作会员(除非特别指出
) 普通初始会员的
例外情况。


类似的评论适用于您的其他代码片段。


-

Ben。

For the fastest, try:

printf("0x%08lx\n", mylong); /* :-) */

Versions B and C, invoke undefined behaviour. The defined way to do
version B is:

void *vp = &mylong;
unsigned char *cp = vp;
/* now do what you want with cp[0] to cp[sizeof long] */

There is no need to lie about having an array. Version C is very
likely to work, but the standard does not guarantee accesses to any
union member other than the last one assigned to (barring the special
exception for "common initial members").

Similar comments apply to the your other code fragments.

--
Ben.