Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

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.

Is it maybe worth Cythonizing the function? Is it even worth using the numpy array, or am I most-likely prematurely optimizing?

The arg string/list looks like this:

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

In the case of Python programs, the largest start-up cost is initialising the execution environment, particularly the search for imported modules. These will probably far outweigh the execution time of, for example, argparse.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...