When in doubt you can always test the results. In this benchmark all methods are executing the same code. The only difference is that in some I execute everything on one method and on others I split it into multiple methods.
[MemoryDiagnoser]
public class Test
{
[Benchmark]
public async Task TestWithoutCallingOtherMethod()
{
// call an async method to make this method async
await Task.Delay(0);
var guid = Guid.NewGuid().ToByteArray();
// function to be extracted ----------------------------------
int sum = 0;
// call an async method to make this method async
await Task.Delay(0);
foreach (var bt in guid)
sum += bt;
// -----------------------------------------------------------
// will never happen. Include so compiler does not complain
if (sum == 100000)
throw new Exception();
}
[Benchmark]
public async Task TestCallingAnotherMethod()
{
// call an async method to make this method async
await Task.Delay(0);
var guid = Guid.NewGuid().ToByteArray();
if (await sumBytes(guid) == 1000000)
throw new Exception();
}
async Task<int> sumBytes(byte[] guid)
{
// call an async method to make this method async
await Task.Delay(0);
int sum = 0;
foreach (var bt in guid)
sum += bt;
return sum;
}
[Benchmark]
public async Task TestCallingAnotherMethodThatIsStatic()
{
// call an async method to make this method async
await Task.Delay(0);
var guid = Guid.NewGuid().ToByteArray();
if (await sumBytesStatic(guid) == 1000000)
throw new Exception();
}
static async Task<int> sumBytesStatic(byte[] guid)
{
// call an async method to make this method async
await Task.Delay(0);
int sum = 0;
foreach (var bt in guid)
sum += bt;
return sum;
}
}
And this where the results:
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|------------------------------------- |---------:|--------:|--------:|-------:|----------:|
| TestWithoutCallingOtherMethod | 343.5 ns | 1.23 ns | 1.02 ns | - | 40 B |
| TestCallingAnotherMethod | 345.6 ns | 6.31 ns | 5.27 ns | 0.0010 | 112 B |
| TestCallingAnotherMethodThatIsStatic | 341.9 ns | 3.09 ns | 4.12 ns | - | 112 B |
This results shows that it is practically the same.