Skip to main content

Function overhead test

For this test I was curious to see the overhead of a function call

I executed two methods Benchmark1 and Benchmark2 and compared their execution time.

private void Func()
{
	int j = 0;
}

public override void Benchmark1()
{
	for (int i = 0; i < 50000; i++)
	{
		int j = 0;
	}
}

public override void Benchmark2()
{
	for (int i = 0; i < 50000; i++)
	{
		Func();
	}
}

Both methods do the same thing, setting a variable j to 0, except that Benchmark2 calls a function.

B1 : 57.036 msĀ 
B2 : 66.207 ms (1.16 times slower)

There's a little difference, calling a function has a little overhead, but nothing too bad.
So in theory, putting everything into a single function is more performant (But obviously that would be a bad programming advice)