Question

This issue touches Go and C# so the comparison would deal with current implementations -- I am rather looking for theoretical evidence, something like quick sort in worst case performance is...

In many places dedicated to Go I saw a function that returns a tuple, with last element being an error code. In C# it is rather TryXXX pattern with last out parameter.

Now I wonder which one is better performer (once again, from theoretical standpoint -- meaning if you could implement it at best, what would be the outcome).

Maybe naive thinking, but with tuple approach I see memory allocation problem, you have to do it each time the function is called, no matter what. With out parameter you allocate memory outside the function, so then even if you call a function millions times, the cost is zero (well, except for copying some data).

out then should be a better performer, correct? Or is there some way to implement tuple passing to match out? Or am I completely wrong?

Was it helpful?

Solution

The way multiple return values are implemented by the current Go suite (gc) the return values are passed the same way parameters are, which are on stack. There is no memory allocation at that point (Assuming the stack size was sufficient, which is another matter). Considering the out parameter in C# would need to be allocated, I'd say the Go approach would be faster but not because of the approach in itself but by the fact that non-primitive data types are heap allocated in C# whereas in Go the programmer can choose to keep it on stack.

If we consider simply the parameter passing versus the multiple return value, the performance impact would be non-existant since both represent a push/pop on the stack.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top