Reputation: 51927
Is there a difference in terms of performance between these two ways of writing code that is functionally equivalent?
Option 1:
SomeObjecModel TheObjectModel = new SomeObjectModel();
return TheObjectModel.SomeMethod();
Option 2:
return new SomeObjectModel().SomeMethod();
In option 1 the object is instantiated into a variable whereas in option 2 there's no variable. I'm just curious if there's any difference.
Upvotes: 3
Views: 91
Reputation: 3751
There won't be a performance difference. Because if you check that IL code, it is giving a name to the constructor after creating it. In any case, you should prefer which one is more readable.
Option1:
IL_0000: newobj UserQuery+SomeObjectModel..ctor
IL_0005: stloc.0 // TheObjectModel
IL_0006: ldloc.0 // TheObjectModel
IL_0007: callvirt UserQuery+SomeObjectModel.SomeMethod
IL_000C: ret
Option2:
IL_0000: newobj UserQuery+SomeObjectModel..ctor
IL_0005: call UserQuery+SomeObjectModel.SomeMethod
IL_000A: ret
Before callvirt was used, it was possible to call a method on a null instance of a class. To avoid null calls callvirt has been created in the first option.
Upvotes: 0
Reputation: 3563
There is no difference between these two methods of writing code. In first option GC
will collect the variable after the scope. It is not a matter what style you choose to write the code and the compiler would convert and compile to lower level code from higher level.
eg: Yoy can use Delegate
instead of Action
and both are serving the same functionality but the coding style is different. So compiler will always convert this Action
(higlevel) statement into Delegate
(low level)
Upvotes: 0
Reputation: 292405
There is a difference in the generated IL code:
Option1:
IL_0000: newobj UserQuery+SomeObjectModel..ctor
IL_0005: stloc.0 // TheObjectModel
IL_0006: ldloc.0 // TheObjectModel
IL_0007: callvirt UserQuery+SomeObjectModel.SomeMethod
IL_000C: ret
Option2:
IL_0000: newobj UserQuery+SomeObjectModel..ctor
IL_0005: call UserQuery+SomeObjectModel.SomeMethod
IL_000A: ret
(compiled in LinqPad with optimizations turned on)
However it's unlikely to make a noticeable difference at runtime.
Upvotes: 3
Reputation: 62093
There WAS - in case of a breakpoint you had no way to see the value in the debugger (as you could only set a breakpoint BEFORE the return.
So, I often used the first syntax to be able to set a breakpoint on the return and see the value. I would actually even put the SomeMethod() call into a varaible for the exact same reason.
Since VS 2013 Update 1 or 2 the return values are explicitly available in the debugger, so the first syntax is moot.
The update is described here:
Upvotes: 5
Reputation: 564403
There is likely going to be no measurable difference. In both cases, the vast majority of the time spent will be instantiating the object, as well as executing your method.
Use the version you find more readable (and hence maintainable).
Upvotes: 7
Reputation: 10604
Either way you are creating a variable. One is just done inline. I would be more worried about whether that variable needs to be disposed of. If so, I would prefer to see everything wrapped in a 'using' statement.
Upvotes: 2