Try/catch or validation for speed?

后端 未结 2 1193
南笙
南笙 2020-11-30 23:21

I\'m working with Python and whenever I\'ve had to validate function input, I assumed that the input worked, and then caught errors.

In my case, I had a universal

2条回答
  •  孤街浪徒
    2020-11-30 23:44

    In Python, exceptions are often faster due to the reduced number of lookups. However a friend once said (and it should apply to any language), pretend that everytime an exception is caught, there is a small delay. Avoid using exceptions where a delay could be a problem.

    In the example you've given, I'd go with the exception.

提交回复
热议问题