views:

282

answers:

5

Probably a stupid question, but it's an idle curiosity for me.

I've got a bit of Delphi code that looks like this;

const
  KeyRepeatBit = 30;

...
  // if bit 30 of lParam is set, mark this message as handled
  if (Msg.lParam and (1 shl KeyRepeatBit) > 0) then
    Handled:=true;
...

(the purpose of the code isn't really important)

Does the compiler see "(1 shl KeyRepeatBit)" as something that can be computed at compile time, and thus it becomes a constant? If not, would there be anything to gain by working it out as a number and replacing the expression with a number?

+6  A: 

Yes, the compiler evaluates the expression at compile time and uses the result value as a constant. There's no gain in declaring another constant with the result value yourself.

EDIT: The_Fox is correct. Assignable typed constants (see {$J+} compiler directive) are not treated as constants and the expression is evaluated at runtime in that case.

TOndrej
Thanks, that's all I needed. :-)
robsoft
You forgot about writable constants ;) In that case the expression is evaluated at run-time
The_Fox
+3  A: 

It converts it to a constant at compile time.

However, even if it didn't, this would have no noticeable impact on your application's performance.

You might handle a few thousand messages per second if your app is busy. Your old Pentium I can do gazillions of shifts and ands per second.

Keep your code readable, and profile it to find bottlenecks that you then optimize - usually by looking at the algorithm, and not such a low level as whether you're shifting or not.

Will
+2  A: 

I doubt that using a number (would be 1073741824, by the way) here would really improve performance. You seem to be in some Windows message context here and this will possible add more delay than a single and that is lightning fast even if the number is not optimized at compiled time (anyway, I think it is optimized).

The only exception I could imagine would be the case that this particular piece of code is run really often, but as I said I think this gets optimized at compile time and so even in this case it won't make a difference at all.

schnaader
+1 for bothering to work out 1 shl 30! :-)
robsoft
+4  A: 

You can make sure iike this, for readability alone:

const
  KeyRepeatBit = 30;
  KeyRepeatMask = 1 shl KeyRepeatBit ;
Henk Holterman
Why not KeyRepeatMask = 1 shl KeyRepeatBit?
RobS
+1  A: 

Maybe it's offtopic to your question but I use a case record for these kind of things, example:

  TlParamRecord = record
    case Integer of
      0: (
        RepeatCount: Word;
        ScanCode: Byte;
        Flags: Set of (lpfExtended, lpfReserved=4, lpfContextCode,
          lpfPreviousKeyState, lpfTransitionState);
      );
      1: (lParam: LPARAM);
  end;

see article on my blog for more details

Remko