dongzhui4927 2013-01-14 17:57
浏览 14
已采纳

超级嵌套函数的缺点?

On xDebug they limit how many times a function can be nested, are there any disadvantages of nesting functions for so many times?

  • 写回答

1条回答 默认 最新

  • dsg7513 2013-01-14 18:01
    关注

    Yes.

    Each time you call a function from within another function, you add one level to the stack.

    The past function calls have not yet returned, so the memory they claimed for local variables cannot be deallocated; it remains resident until the whole stack is unwound.

    Imagine you were to throw an exception from inside a function 1,000 calls deep. In order to generate a stack trace, it would have to walk upward through 1,000 function calls, putting them all into the exception object.

    There's nothing you can do with recursion that deep which you couldn't do instead with a well-written loop, and the loop would be both more memory-efficient (as it doesn't have to have the stack frame overhead for each function invocation) and time-efficient (as it doesn't have the overhead of actually performing the function calls you removed).

    It is exceedingly unlikely that a program which reaches a recursion level of 1,000 is behaving as intended. More likely, it's stuck in infinite recursion and will eventually exhaust all the system's RAM. Better to kill it earlier.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法