开发者

How to optimize this? Pointer to array

How can I optimize this code? pAmmoOf开发者_C百科fset is pointer to byte array

*pAmmoOffset        = 0x89;
*(pAmmoOffset + 1)  = 0x70;
*(pAmmoOffset + 2)  = 0x04;


Have you measured this code with a profiler and determined it's a bottleneck? If so, sorry, but there's nothing you can do since the compiler has already made it as efficient as it can be.


You could try to pipeline 4 bytes at a time on a 32-bit platform. However, I wouldn't be surprised if attempting to do that manually is ultimately slower than what the compiler generated in the first place.

What you're doing is just about as simple as can be. It's doubtful that anything can be done to optimize this further, unless the code you've provided isn't quite the case and you're not writing compile-time constants into those addresses.


I see hardly any reason to optimize your code. But if you must you could try assigning blocks of values like this:
*(int*) pAmmoOffset = 0x08040201;

which is equivalent to:
*pAmmoOffset = 0x01;
*(pAmmoOffset + 1) = 0x02;
*(pAmmoOffset + 2) = 0x04;
*(pAmmoOffset + 3) = 0x08;

You could assign bigger blocks as well using int64 if you need.


*pAmmoOffset++ = 0x89;
*pAmmoOffset++ = 0x70;
*pAmmoOffst    = 0x04;

Of course, this modifies the pointer.

Also, if the pointer is a global variable, the generated code will re-read it after each write. To get around this, copy it to a local variable and use it in the write statements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜