duanri1904
2014-03-18 14:04
浏览 79

返回错误代码是否比out参数慢?

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?

图片转代码服务由CSDN问答提供 功能建议

此问题涉及Go和C#,因此比较将处理当前实现 -我宁愿寻找理论证据,例如在最坏情况下的快速排序等...

在很多专门用于Go的地方,我看到了一个函数返回 元组,最后一个元素是错误代码。 在C#中,它是带有最后一个 out 参数的 TryXXX 模式。

现在,我想知道哪个是性能更好的(再次,从 理论上的观点-意味着如果您最多能实现它,结果将是什么)。

也许是幼稚的想法,但是使用元组方法,我看到了内存分配问题,您必须这样做 每次调用该函数,无论如何。 使用 out 参数可以在函数外部分配内存,因此,即使您调用函数数百万次,开销也为零(很好,除了复制某些数据外)。

out 然后应该是表现更好的,对吗? 还是有某种方法可以实现元组传递以匹配 out ? 还是我完全错了?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyi3776 2014-03-18 18:34
    已采纳

    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.

    打赏 评论

相关推荐 更多相似问题