-3

There is a very similar question to what I intend to ask here but the answers in that question diverted to Java world instead of C# world and also none of the answers over there talk about the micro-optimization of performance, if any.

is there any difference in performance, even a tiny one, in the following two statement blocks?

var someObject = new UpdateCommand
{
  Name = "StackOverflow",
  Age = 20,
  IsPrimaryWebsite = true,
  MobileNumber = "12345678"
};
Mediator.Send(someObject);

and

Mediator.Send(new UpdateCommand
{
  Name = "StackOverflow",
  Age = 20,
  IsPrimaryWebsite = true,
  MobileNumber = "12345678"
});

I assume the latter is automatically converted to the former by the compiler but want to verify it.

4
  • 4
    You can look at the il and see for yourself Commented Jan 9, 2024 at 0:45
  • 5
    Which is more readable to you? That's the only thing that matters. If you're asking about how something performs, you haven't done the tests and it doesn't actually matter. Commented Jan 9, 2024 at 0:45
  • 2
    It depends on the build configuration used. If you want to verify a particular compiler and configuration: disassemble the output from that compiler. Commented Jan 9, 2024 at 0:48
  • Almost certainly the result is the same. The JIT will place variables either on the stack or in a register, and entirely stack-bound objects (such as the second option) will be the same. The differences will be so minor as to be negligible except in exceedingly high performance code. Commented Jan 9, 2024 at 16:35

1 Answer 1

-1

The first example "will" be slightly slower because it will get compiled with an additional stloc.0 followed by a ldloc.0 between the creation of the object and calling Mediator.Send(). This however is only a theoretical difference in practical speed of processing between the two. Most CPUs on the market will speculatively execute the Mediator.Send() command with the information it already has loaded into the register without executing the stloc.0 and ldloc.0 commands from the CIL.

Even if the CPU does not speculatively execute this, nearly all CPUs made in the last 10 years would execute both IL command lists for both methods in the same exact amount of time because it would take less than 1 clock cycle to complete either of them. The only actual variable in terms of timing performance will be the inherent pseudo-randomness you will encounter in terms of CPU core scheduling, CPU core temperature (even a fraction of a fraction of a degree will make changes at this small of an execution), and the amount of work being performed for literally everything else on the machine.

So basically the answer is: the first example technically compiles into more work, but the CPU will likely execute the Mediator.Send() call speculatively using the work it just performed to avoid the need to store and call data in a register. Even if it doesn't do this, the entirety of the instructions provided to the CPU will almost certainly take less than 1 clock cycle so the differences in performance will appear inherently random, or might be due to some higher level issues that are outside of your control and will be different from machine to machine. The performance impact will be the same between the two.

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

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.