Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of any()

Tags:

python

This is mostly an exercise in learning Python. I wrote this function to test if a number is prime:

def p1(n):
    for d in xrange(2, int(math.sqrt(n)) + 1):
        if n % d == 0:
            return False
    return True

Then I realized I can make easily rewrite it using any():

def p2(n):
    return not any((n % d == 0) for d in xrange(2, int(math.sqrt(n)) + 1))

Performance-wise, I was expecting p2 to be faster than, or at the very least as fast as, p1 because any() is builtin, but for a large-ish prime, p2 is quite a bit slower:

$ python -m timeit -n 100000 -s "import test" "test.p1(999983)"
100000 loops, best of 3: 60.2 usec per loop

$ python -m timeit -n 100000 -s "import test" "test.p2(999983)"
100000 loops, best of 3: 88.1 usec per loop

Am I using any() incorrectly here? Is there a way to write this function using any() so that it's as far as iterating myself?

Update: Numbers for an even larger prime

$  python -m timeit -n 1000 -s "import test" "test.p1(9999999999971)"
1000 loops, best of 3: 181 msec per loop

$  python -m timeit -n 1000 -s "import test" "test.p2(9999999999971)"
1000 loops, best of 3: 261 msec per loop
like image 369
linus Avatar asked Oct 26 '14 22:10

linus


People also ask

What is a performance review?

A performance review is a regulated assessment in which managers assess an employee’s work performance to identify their strengths and weaknesses, offer feedback and assist with goal setting. The frequency and depth of the review process may vary by company based on company size and goals of the evaluations.

How effective is the everyday performance management system?

The Everyday Performance Management system had overwhelmingly positive results, with 69% of employees stating that they received feedback that was useful for their professional development, and 70% reporting that they felt valued as a result of the continuous performance discussions with their manager.

Are quarterly performance reviews a good idea?

But if you take the right approach, quarterly performance reviews are an awesome opportunity to reinforce solid habits, redirect poor traits and drive professional growth for your employees.

What should you say at your next performance review?

Making it personal; feedback is about actions and behaviour, not the person. Loaded language; focus on asking what and how, not why. Enquiring why someone acted the way they did is akin to searching for a ‘motive’ and may come across accusatory. Grappling with what to say at your next performance review?


1 Answers

The performance difference is minimal, but the reason it exists is that any incurs building a generator expression, and an extra function call, compared to the for loop. Both have identical behaviors, though (shortcut evaluation).

As the size of your input grows, the difference won't diminish (I was wrong) because you're using a generator expression, and iterating over it requires calling a method (.next()) on it and an extra stack frame. any does that under the hood, of course.

The for loop is iterating over an xrange object. any is iterating over a generator expression, which itself is iterating over an xrange object.

Either way, use whichever produces the most readable/maintainable code. Choosing one over the other will have little, if any, performance impact on whatever program you're writing.

like image 79
Max Noel Avatar answered Sep 18 '22 09:09

Max Noel