Testing, profiling and optimization Flashcards

1
Q

pytest of factorial(order)

A

import pytest
from script import factorial

def test_factorial():
    assert factorial(3) == 6

if __name__ == “__main__”:
pytest.main(“thisfile.py”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

doctest of factorial(order)

A
def factorial(order):
    """
>>> factorial(3)
6
    """
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

profile factorial(6)

A

import cProfile
import pstats

from script import factorial

cProfile.run(“factorial(6)”, “factorial”)
pstats.Stats(“factorial”).print_stats()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

time factorial(6)

A

from timeit import timeit

timeit(“factorial(6)”, “from script import factorial”, number=100000)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

time a block of code

A

from time import time

timer = time()
# Do something
timer = time() - timer
print timer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

optimization in python

A

Local name space > Global name space
if-elif-else > try-except-default
Few function arguments > Many function arguments
xrange > range
Python native initialisation > Numpy initialisation
Numpy iteration > Python native iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

weave

A

from scipy import weave

weave.inline(“””//code”””, [‘var1’, ‘var2’], type_converters = weave.converters.blitz, headers = [’’])

NB! Primitive types are copied (I think)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly