0

How can I optimize this code? pAmmoOffset is pointer to byte array

*pAmmoOffset        = 0x89;
*(pAmmoOffset + 1)  = 0x70;
*(pAmmoOffset + 2)  = 0x04;
4
  • 4
    Theres nothing to optimize here. What will be best in the end is up to the compiler. Just make sure you compile with optimization flags. Commented Mar 4, 2011 at 14:14
  • In what way exactly would you want to optimize this? i.e., what would you want to improve? Commented Mar 4, 2011 at 14:15
  • 1
    Have you already optimized the rest of your program? Is this piece of code really the bottleneck? This is pretty much optimised and the compiler will anyway do it better than you when specifying which processor you are targetting. Commented Mar 4, 2011 at 14:19
  • You could change the style to use array notation, e.g. square brackets instead of the pointer notation; this won't affect performance though. Commented Mar 4, 2011 at 17:45

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

+1 for "I wouldn't be surprised if attempting to do that manually is ultimately slower than what the compiler generated in the first place." Trying to outsmart the compiler on specific code-generation optimizations is almost always a losing game. I'm tied of code comments saying "for performance" on some bizarre unreadable construct, and you profile it and it's slower than the simple code.
1

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.

2 Comments

This violates the strict aliasing rules, and also depends on the byte order of the underlying hardware. Don't try this a home!
Gaa! I wasn't aware of these rules. Thanks for pointing them out.
0
*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.

1 Comment

This is likely to be slower than the sample code. The function '++' has to store the old value, increment the current value, and then return back the old value. Of course, this is specific to the compiler and the architecture. I've seen code like *(q++) = *(p++) evaluate to one machine instruction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.