Dee
Dee

Reputation: 191

Better / faster Python code

Yet another noob query here. Quotes like "never write the same code twice" have me trying to ferret out my mistakes; here's one I want to improve: code in main file:

if os.name == 'nt':
    dosomething

another way:

if os.name == 'nt':
    os_is_nt = True

if os_is_nt:
    dosomething

another way, putting this function in an import

detectNT()
    if os.name == 'nt':
        return True
    else:
        return False

My belief is, without figuring out how to check any speed differences, is that I should cut it back to the original:

if os.name == 'nt':
    dosomething

but my faith is weak, which is why I'm here.

Also, I'm thinking there's a way to find the local file separator..? I've written this:

def os_file_sep():
    file_separator = "/"
    if os.name == 'nt':
        file_separator = "\\"
    return file_separator

Upvotes: 0

Views: 73

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148880

Your general question is about performance, so I will answer to that.

The main rule is: do not worry about low level performance unless you really need it. That means that you should be careful about algorithms but not about low level optimization at first sight, and always prefer simple and maintainable code.

When you have a performance problem (or suspect you will soon have), profile your code. Python provides useful tools out of the box in the Standard Library. One of them is profile or its optimized version cProfile. Extract from the manual:

To profile an application with a main entry point of foo(), you would add the following to your module:

import cProfile
cProfile.run('foo()')

The above action would cause foo() to be run, and a series of informative lines (the profile) to be printed.

Now you know where your program spend most of its time and you know the parts that deserve low level optimisation.

And here again Python provides out of the box the timeit module. This module provides a simple way to time small bits of Python code. It has both command line as well as callable interfaces. It avoids a number of common traps for measuring execution times.

For the precise question you asked, the correct answer is David Mašek's one. This one is for the general question of performance. Anyway, if you have doubts about the performances of the different solutions (yours and David's ones), just use timeit to be sure, and optionally report here in a self answer.

Upvotes: 1

David Mašek
David Mašek

Reputation: 922

What should you use?

Use os.path.join (or os.path.sep). Don't reinvent wheel. It's simple and great.

>>>os.path.join("foo", "bar")
'foo\\bar'

What if there wasn't this (better) way?

  • Value never changes - first way ("direct" if check) is IMO best. It's simple and readable (unless you do this check in many different places, then I guess variable could be more error prone).
  • Value changes - I would choose variable and if check if the check is simple and function if the check is complicated.

Upvotes: 1

Related Questions