729,

730,

731,

732,

733,

734,

735,

736,

737,

738,

739,

740,

741,

742,

743,

744,

745,

746,

747,

748,

749,

750,

751,

752,

753,

754,

755,

756,

757,

758,

759,

760,

761,

762,

763,

764,

765,

766,

767,

768,

769,

770,

771,

772,

773,

774,

775,

776,

777,

778,

779,

780,

781,

782,

783,

784,

785,

786,

787,

788,

789,

790,

791,

792,

793,

794,

795,

796,

797,

798,

799,

800,

801,

802,

803,

804,

805,

806,

807,

808,

809,

810,

811,

812,

813,

814,

815,

816,

817,

818,

819,

820,

821,

822,

823,

824,

825,

826,

827,

828,

829,

830,

831,

832,

833,

834,

835,

836,

837,

838,

839,

840,

841,

842,

843,

844,

845,

846,

847,

848,

849,

850,

851,

852,

853,

854,

855,

856,

857,

858,

859,

860,

861,

862,

863,

864,

865,

866,

867,

868,

869,

870,

871,

872,

873,

874,

875,

876,

877,

878,

879,

880,

881,

882,

883,

884,

885,

886,

887,

888,

889,

890,

891,

892,

893,

894,

895,

896,

897,

898,

899,

900,

901,

902,

903,

904,

905,

906,

907,

908,

909,

910,

911,

912,

913,

914,

915,

916,

917,

918,

919,

920,

921,

922,

923,

924,

925,

926,

927,

928,

929,

930,

931,

932,

933,

934,

935,

936,

937,

938,

939,

940,

941,

942,

943,

944,

945,

946,

947,

948,

949,

950,

951,

952,

953,

954,

955,

956,

957,

958,

959,

960,

961,

962,

963,

964,

965,

966,

967,

968,

969,

970,

971,

972,

973,

974,

975,

976,

977,

978,

979,

980,

981,

982,

983,

984,

985,

986,

987,

988,

989,

990,

991,

992,

993,

994,

995,

996,

997,

998,

999]


In [143]: for i in range(3):

     ...: animal = animals[i]

     ...: print(animal)

     ...:

mouse

cat

dog


In [144]: range(3)

Out[144]: range(0, 3)


In [145]: type(range(3))

Out[145]: range


In [146]: range(10)

Out[146]: range(0, 10)


In [147]: str(range(10))

Out[147]: 'range(0, 10)'


In [148]: r = range(10)


In [149]: r

Out[149]: range(0, 10)


In [150]: r[0]

Out[150]: 0


In [151]: r[0:5]

Out[151]: range(0, 5)


In [152]: list(r[0:5])

Out[152]: [0, 1, 2, 3, 4]


In [153]: tuple(r[:5])

Out[153]: (0, 1, 2, 3, 4)


In [154]: d

Traceback (most recent call last):


File "<ipython-input-154-e29311f6f1bf>", line 1, in <module>

d


NameError: name 'd' is not defined



In [155]: d = "Hello"


In [156]: for x in d:

     ...: print(x)

     ...:

H

e

l

l

o


In [157]: for x in [d]:

     ...: print(x)

     ...:

Hello


In [158]: runfile('/Users/progprim/Desktop/rangedouble.py', wdir='/Users/progprim/Desktop')


In [159]: range_double(4)

0

2

4

6


In [160]: runfile('/Users/progprim/Desktop/rangedouble.py', wdir='/Users/progprim/Desktop')


In [161]: range_double(4)

0

2

4

6


In [162]: runfile('/Users/progprim/Desktop/rangedouble.py', wdir='/Users/progprim/Desktop')


In [163]: range_double(4)


In [164]: runfile('/Users/progprim/Desktop/rangedouble.py', wdir='/Users/progprim/Desktop')


In [165]: range_double(4)

Out[165]: [0]


In [166]: runfile('/Users/progprim/Desktop/rangedouble.py', wdir='/Users/progprim/Desktop')


In [167]: range_double(4)

Out[167]: [0, 2, 4, 6]


In [168]: range_double(10)

Out[168]: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]


In [169]: runfile('/Users/progprim/Desktop/rangedouble.py', wdir='/Users/progprim/Desktop')


In [170]: range_double2(4)

Out[170]: [0, 2, 4, 6]


In [171]: range_double(4)

Out[171]: [0, 2, 4, 6]


In [172]: list(range(0, 10, 3))

Out[172]: [0, 3, 6, 9]


In [173]: %timeit range_double(1000)

10000 loops, best of 3: 124 µs per loop


In [174]: %timeit range_double2(1000)

100000 loops, best of 3: 17.8 µs per loop


In [175]: %timeit?

Docstring:

Time execution of a Python statement or expression


Usage, in line mode:

%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement

or in cell mode:

%%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code

code

code...


Time execution of a Python statement or expression using the timeit

module. This function can be used both as a line and cell magic:


- In line mode you can time a single-line statement (though multiple

ones can be chained with using semicolons).


- In cell mode, the statement in the first line is used as setup code

(executed but not timed) and the body of the cell is timed. The cell

body has access to any variables created in the setup code.


Options:

-n<N>: execute the given statement <N> times in a loop. If this value

is not given, a fitting value is chosen.


-r<R>: repeat the loop iteration <R> times and take the best result.

Default: 3


-t: use time.time to measure the time, which is the default on Unix.

This function measures wall time.


-c: use time.clock to measure the time, which is the default on

Windows and measures wall time. On Unix, resource.getrusage is used

instead and returns the CPU user time.


-p<P>: use a precision of <P> digits to display the timing result.

Default: 3


-q: Quiet, do not print result.


-o: return a TimeitResult that can be stored in a variable to inspect

the result in more details.



Examples

--------

::


In [1]: %timeit pass

10000000 loops, best of 3: 53.3 ns per loop


In [2]: u = None


In [3]: %timeit u is None

10000000 loops, best of 3: 184 ns per loop


In [4]: %timeit -r 4 u == None

1000000 loops, best of 4: 242 ns per loop


In [5]: import time


In [6]: %timeit -n1 time.sleep(2)

1 loop, best of 3: 2 s per loop



The times reported by %timeit will be slightly higher than those

reported by the timeit.py script when variables are accessed. This is

due to the fact that %timeit executes the statement in the namespace

of the shell, compared with timeit.py, which uses a single setup

statement to import function or create variables. Generally, the bias

does not matter as long as results from timeit.py are not mixed with

those from %timeit.

File: ~/anaconda/lib/python3.5/site-packages/IPython/core/magics/execution.py


In [176]: %timeit range_double(10)

100000 loops, best of 3: 1.99 µs per loop


In [177]: %timeit range_double2(10)

The slowest run took 7.08 times longer than the fastest. This could mean that an intermediate result is being cached.

1000000 loops, best of 3: 784 ns per loop


In [178]: %timeit range_double2(10)

The slowest run took 5.22 times longer than the fastest. This could mean that an intermediate result is being cached.

1000000 loops, best of 3: 776 ns per loop


In [179]: 2 * range(5)

Traceback (most recent call last):


File "<ipython-input-179-7b69ad173039>", line 1, in <module>

2 * range(5)


TypeError: unsupported operand type(s) for *: 'int' and 'range'



In [180]: 2 * list(range(5))

Out[180]: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]


In [181]: runfile('/Users/progprim/Desktop/rangedouble.py', wdir='/Users/progprim/Desktop')


In [182]: range_double(4)

Out[182]: <list_iterator at 0x119f4f080>


In [183]: list(range_double(4))

Out[183]: [0, 2, 4, 6]


In [184]: range_double2(4)

Out[184]: range(0, 8, 2)


In [185]: %timeit range_double(1000)

10000 loops, best of 3: 126 µs per loop


In [186]: %timeit range_double2(1000)

The slowest run took 5.14 times longer than the fastest. This could mean that an intermediate result is being cached.

1000000 loops, best of 3: 479 ns per loop


In [187]: