简体   繁体   English

在 Python 中解析 arguments 的最快方法是什么?

[英]What's the fastest way to parse arguments in Python?

What is the fastest way to pass a short list of command line options?传递简短命令行选项列表的最快方法是什么?

I started with just a list, then thought about using a numpy array, because as I understand it then the items in the array are all pre-typed and stored more efficiently in memory.我从一个列表开始,然后考虑使用 numpy 数组,因为据我了解,数组中的项目都是预先输入的,并且更有效地存储在 memory 中。

Is it maybe worth Cythonizing the function?是否值得对 function 进行 Cythonizing? Is it even worth using the numpy array, or am I most-likely prematurely optimizing?甚至值得使用 numpy 阵列,还是我很可能过早优化?

The arg string/list looks like this: arg 字符串/列表如下所示:

ARG_STRING = 'cdehinu:'
ARG_LIST = ['credentials',
            'delete_user',
            'email',
            'help',
            'ip',
            'notifications',
            'user_load=']

And a minimal example of the parsing function looks like this:解析 function 的最小示例如下所示:

def arg_parse(self, opts):

    for opt, arg in np.array(opts):     # here I have added the numpy conversion
        if opt in {'-i', '--ip'}:
            do.something()
        elif opt in {-h, '--help}:
            do.something_else()
        ...

I know at the end of the day, it's all rather unnecessary, and my feeling is that converting it to an array is going to take as much if not more time than it saves in this small list, but in the interests of best (and/or fastest) practices, I'd love to hear some more experienced people's thoughts on the matter, or other ideas how this process could be optimized.我知道归根结底,这一切都是不必要的,而且我的感觉是,将其转换为数组所花费的时间与在这个小列表中节省的时间一样多(如果不是更多的话),但为了最好的(和/ 或最快)的做法,我很想听听一些更有经验的人对此事的看法,或其他如何优化此过程的想法。

Thanks in advance.提前致谢。

Argument parsing happens exactly once in a typical application.参数解析在典型应用程序中只发生一次。 Unless the application is trivial, it's unlikely that argument parsing will have any noticeable impact on execution time, and much less if it's a "short list of command line options".除非应用程序是微不足道的,否则参数解析不太可能对执行时间产生任何明显影响,如果它是“命令行选项的简短列表”,则更少。

What you should optimise for in argument parsing is usability .在参数解析中应该优化的是可用性 That includes conforming to usual command-line conventions as well as providing good and accessible documentation and comprehensive verification of argument correctness.这包括遵守通常的命令行约定以及提供良好且易于访问的文档以及对参数正确性的全面验证。

Established argument parsing libraries tend to focus on these goals, sometimes even at the cost of slightly increased runtime.已建立的参数解析库倾向于关注这些目标,有时甚至会以略微增加运行时间为代价。 But the few microseconds incurred per invocation will be amply repaid the first time a good usage message saves a long and frustrating script debugging session.但是每次调用所产生的几微秒将在第一次使用良好消息时得到充分回报,从而节省了调试 session 的冗长且令人沮丧的脚本。

In the case of Python programs, the largest start-up cost is initialising the execution environment, particularly the search for imported modules.在 Python 程序的情况下,最大的启动成本是初始化执行环境,特别是搜索导入的模块。 These will probably far outweigh the execution time of, for example, argparse .这些可能远远超过例如argparse的执行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM