3

I have some libraries that contain a large number of constants. Any application that uses the library will use some or none of those constants. So, I was wondering whether using lambda expressions (for example):

public static Milliseconds {
    public static int OneSecond => 1000;
    public static int TwoSeconds => 2000;
    .
    .
}

would be more efficient either in terms of file sizes (exe or dll) or run-time speed than:

public static Milliseconds {
    public const int OneSecond = 1000;
    public const int TwoSeconds = 2000;
    .
    .
}

I'm sure any differences would be minimal. I'm not looking to squeeze the last byte or nano-second out, I'm just curious. Thank you.

9
  • Try it out..... Commented Sep 7, 2017 at 9:37
  • Have you tried to measure it? Commented Sep 7, 2017 at 9:37
  • 7
    This sounds like something that you're in a better position to test than anyone else. Are applications likely to use these in tight loops? If so, have you tested what the impact is? Are they likely to want to use those constants in other constant expressions? Also note that your code doesn't contain any lambda expressions. It contains expression-bodied properties. There are no delegates or expression trees involved. Commented Sep 7, 2017 at 9:37
  • 2
    Constants in C# are propagated by the compiler. If you change your const values then any consuming libraries will need to be recompiled also. From the context of your values this looks like it would be unlikely for you but it's something that has caught me out in the past. Commented Sep 7, 2017 at 9:41
  • 1
    But because the constant value is propagated, the compiler will actually insert the constant value into the consuming library's code, so const will be faster at run time because the calling library won't even go into your assembly afaik. Commented Sep 7, 2017 at 9:43

2 Answers 2

4

Property approach actually creates methods like get_OneSecond() which return a number which is stored in your assembly.

The second const approach does not create any members, it inlines the value of your constants wherever you use it at compile-time.

So, approach 1 will take more space and will be less "efficient", i.e. require more instructions to be executed. Of course, we talk about unnoticeable and tiny differences.

However, at the same time approach 1 gives you two things:

  • It gives more flexibility allowing you to encapsulate your logic. For example, one day you can make OneSecond be acquired another way (loaded from configuration / calculated / etc.) instead of being constant. These changes will not change abstraction and affect someone who use your Milliseconds class.

  • It lets you update your values by replacing DLL. If you use constants and replace the DLL which contains your Milliseconds class, it won't work, since constants are inlined - you will have to rebuild the whole project.

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

2 Comments

public static readonly int OneSecond = 1000; (readonly instead of const) is more readable then approach 1 when we want flexibility
@DmitryBychenko Thank you! Yes, it looks more appropriate in case if you we are sure that this value is unmodifiable. If there is a chance, that someday we will need to calculate its value on each getter access or according to some external values (like current thread ID or authenticated user name etc.), then use property.
1

In response to one of the comments I received, I have now bench-marked this. I had assumed that the compiler would be able to inline and optimise a lambda expression so in this case the two would be virtually the same, but it turns out I was wrong.

My benchmark just confirms the answer from Yeldar Kurmangaliyev. Using lambda expressions in place of constants both increases the size of the dll that they are in and the size of the final exe, as well as having a detrimental affect of performance. This is true for both debug and release builds.

My benchmark used 4,500 constants or lambda expressions, constructing an array from them and then did some simple maths on the array and then repeated the process a hundred thousand times.

Using consts:

dll size was 94kb

exe size was 22kb

benchmark took 2 seconds.

Using lambdas:

dll size was 173kb

exe size was 115kb

benchamrk took 17 seconds.

Timings for both were improved running release over debug (0.6 seconds and 9.5 seconds). Your figures may vary, I've only included mine as a guide.

So, I'll stick with const for constants.

Comments

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.