gpt4 book ai didi

python - 字符串到(可能的)整数。错误处理 : Filter with regex or try. ..catch?

转载 作者:行者123 更新时间:2023-12-03 09:08:12 27 4
gpt4 key购买 nike

坐在这个问题面前,我不确定我应该选择哪条路。

我得到代表 ID 的字符串输入。
大多数情况下,在表/字典中查找实际 ID 是有意义的键名。
但也应该可以直接输入整数 ID。但这些将作为一个字符串出现。

我不确定这是否更像是一个风格问题,或者是否有一个巨大的倾向。

选项1:
使用正则表达式,检查整数,
如果是真的转换。

选项 2:

try 
ID = string.tointeger()
catch
ID = table[string]

我倾向于 try...catch 选项,因为它看起来更干净。但我不确定是否通过正则表达式避免错误处理实际上是更清洁的方式,应该是首选。

对 try...catch 没有更深入的了解;如果它实际上非常顺利“如果你喜欢就使用它”或打嗝“如果你可以避免”

最佳答案

"He who would sacrifice correctness for performance deserves neither[!?]."



TL;博士:
try...catch可以非常快。看起来比试图绕过错误的代码更干净。另一方面,当有大代码块并且在幕后发生你可能不想拥有的事情时,其他人可能更难遵循。
这当然取决于语言

正则表达式可能会非常慢并且是一个廉价的过滤器,然后才能为您节省。

--

所以现在试着自己回答这个问题:

我希望得到一个一般性的答案,但最终它当然取决于语言。
在做研究时,我经常发现它的参数很昂贵,创建异常对象,收集调用堆栈......当然是有道理的。但通常没有任何解释,感觉更像是一种耻辱或迷信:try...catch 是不好的。

根据我的测试(C++):try...catch 方法是最快的。执行速度仅下降 3%。两个简单的“字符串是否以数字开头?” "^-?\d+"整数和浮点数的正则表达式下降了 50%,当我在这些 50%^x 变得明显之后进行一些子字符串分析时。

最后偶然发现了 Bjarne Stroustrup(创建者或 C++)自己的常见问题解答:
http://www.stroustrup.com/bs_faq2.html#exceptions-why

What good can using exceptions do for me? The basic answer is: Using exceptions for error handling makes you code simpler, cleaner, and less likely to miss errors. But what's wrong with "good old errno and if-statements"? The basic answer is: Using those, your error handling and your normal code are closely intertwined. That way, your code gets messy and it becomes hard to ensure that you have dealt with all errors (think "spaghetti code" or a "rat's nest of tests"). [...]

Common objections to the use of exceptions: but exceptions are expensive!: Not really. Modern C++ implementations reduce the overhead of using exceptions to a few percent (say, 3%) and that's compared to no error handling. Writing code with error-return codes and tests is not free either. As a rule of thumb, exception handling is extremely cheap when you don't throw an exception. It costs nothing on some implementations. All the cost is incurred when you throw an exception: that is, "normal code" is faster than code using error-return codes and tests. You incur cost only when you have an error.



那么现在最后我决定在正则表达式之前使用简单的手动过滤器 1st Char in array[-,0-9] 。这可能比 try...catch 慢 10%,但 80% 的时间不会抛出错误。仍然很好的性能和漂亮的代码:)

来自 Python 词汇表: https://docs.python.org/3/glossary.html#term-eafp

EAFP Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

LBYL Look before you leap. This coding style explicitly tests for pre-conditions before making calls or lookups. This style contrasts with the EAFP approach and is characterized by the presence of many if statements. In a multi-threaded environment, the LBYL approach can risk introducing a race condition between “the looking” and “the leaping”. For example, the code, if key in mapping: return mapping[key] can fail if another thread removes key from mapping after the test, but before the lookup. This issue can be solved with locks or by using the EAFP approach.

关于python - 字符串到(可能的)整数。错误处理 : Filter with regex or try. ..catch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59593345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com