5

Is there a performance cost to encapsulating methods? A very brief, arbitrary example:

        public static decimal Floor(decimal value)
        {
            return Math.Floor(value);
        }

Would the above function be inlined? And if so, would it be the exact same as calling Math.Floor() from the code? I did Google before writing this.

Krythic
  • 4,184
  • 5
  • 26
  • 67
  • 2
    Why would you to this ? – Kevin Avignon Sep 19 '15 at 01:05
  • @KevinAvignon Writing a MathHelper class, with your own functions and the .NET ones, too. Basically a one-stop-shop. – Krythic Sep 19 '15 at 01:06
  • 6
    ***If*** there is a performance overhead should you really be concerned ? Another way to put it ; do you currently have a performance bottleneck which has been profiled and the origin is this exact kind of method ? – Sehnsucht Sep 19 '15 at 01:12
  • 2
    @Sehnsucht Nope. I was just being curious; no harm in asking, right? – Krythic Sep 19 '15 at 01:13
  • 2
    @Krythic: Yes, absolutely no harm in asking. And people should point you in the right direction. :) – displayName Sep 19 '15 at 01:34
  • 1
    @displayName but people did not... Right direction: read [horses](http://ericlippert.com/2012/12/17/performance-rant/) by Eric Lippert. Then read something about inlining in C# (like http://stackoverflow.com/questions/616779/can-i-check-if-the-c-sharp-compiler-inlined-a-method-call). Than measure and update your question if you still have one (or self-answer). – Alexei Levenkov Sep 19 '15 at 01:57
  • @AlexeiLevenkov: With all due respect to Eric Lippert, I didn't even bring him into the discussion here. It is not about what anyone said. I have read his 'horses' article. IMO, he wrote it to discourage people from performing micro-optimizations and wasn't meaning to avert people from knowing the cost of a method call. – displayName Sep 19 '15 at 02:23
  • 1
    @AlexeiLevenkov: In fact, the best way to avoid people from performing micro-optimizations is to inform them about the throw-away cost of it. – displayName Sep 19 '15 at 02:29
  • @KevinAvignon Here is also an example where someone did what I was talking about(look to the bottom of the page) http://svn.soapboxcore.com/svn/trunk/Physics2D/AdvanceMath/MathHelper.cs – Krythic Sep 19 '15 at 03:03

1 Answers1

8

Method likely will be inlined (at JIT time, C# compiler does not inline method in IL). Even if not cost is unlikely to impact your overall program. Since optimization and performance numbers are specific to particular code/application you need to measure your case if you see performance problem.

In particular Writing Faster Managed Code: Know What Things Cost article on MSDN gives following estimate for cost of method call: max 6.8 nano-seconds (for 2003 level machine) if the call is not optimized.

Consider reading the rest of the article. In particular Table 3 talks about not only the cost of method calls, but also how much do the operations as trivial as addition, subtraction, multiplication and division cost.

If you need to confirm whether method is inlined - it is covered in many SO questions like Can I check if the C# compiler inlined a method call?

Community
  • 1
  • 1
displayName
  • 13,888
  • 8
  • 60
  • 75
  • "still if you want to know such a trivial thing, you should be informed." and "First make a working software then do optimizations instead of doing premature optimizations. but I sincerely feel that's a bad answer." Upvote for both. – Krythic Sep 19 '15 at 01:42
  • displayName, you should consider providing something that is close to an answer... (And maybe remove meta comments about other possible answers). – Alexei Levenkov Sep 19 '15 at 02:00
  • @AlexeiLevenkov: Updated my answer. Requesting a great guy's up-vote if he is happy now. :D – displayName Sep 19 '15 at 02:43
  • @displayName I've "slightly" edited your post to show what I think would answer the question by rewording information you've provided. My main concern was 6.8ns looked like just taken from thin air... Feel free to roll back. – Alexei Levenkov Sep 19 '15 at 03:18