Timing your code¶
Throughout the semester, we will highlight ways to make code faster. It is almost always faster to stay away from using for loops
; solutions generally involve using clever options associated with arrays to do quick math. Below are some examples to illustrate this from the first problem in Lab 1.
import numpy as np
import time
mean = 10
sigma = 2
In Lab 1, Q1 asks you to generate a 1000x1000 array of random numbers. There are many ways to do this correctly, but some are faster than others.
Here are 3 methods for generating the array in Q1. I will use the command ‘%%timeit’ which runs a cell several times to determine the average run time. Note than when you run a cell using this comand, you’ll have to wait ~factor of 10 longer than its normal runtime to give the timeit
module enough loops to get a steady average.
Method 1¶
%%timeit
arr = np.empty((1000,1000))
for x in range (1000):
val = np.random.normal(mean,sigma,1000)
arr[x] = val
23.5 ms ± 502 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Method 2¶
%%timeit
array = np.array([[np.random.normal(mean, sigma) for i in range(1000)] for j in range(1000)])
2.09 s ± 17.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Method 3¶
%%timeit
array = np.random.normal(mean, sigma, size = (1000,1000))
18.4 ms ± 97.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Method 1 looks like a for loop
, yet it is only marginally slower than Method 3 which doesn’t use a for loop
. Method 2 looks like it could be fast, but the double for loop makes it 100 times slower!!
One reason for this is that Method one loops over only one axis (rows, in this case), and creating a 1000 length long random array is very fast, even if you do it 1000 times. But Method 2, which loops over every pixel, has to run the generator 1000x1000 = 1,000,000 times!
For pieces of code that are hard to isolate, you can also determine run time using import time
. Below I use both to explicitly show what %%timeit is doing. The cell below is run many times, and you can see the different runtimes for the same command.
%%timeit
t0 = time.time()
array = np.random.normal(mean, sigma, size = (1000,1000))
t1 = time.time()
print('Time = {:0.5f}'.format(t1-t0))
Time = 0.02817
Time = 0.02477
Time = 0.01990
Time = 0.01894
Time = 0.01897
Time = 0.01885
Time = 0.01870
Time = 0.01882
Time = 0.02057
Time = 0.01901
Time = 0.01888
Time = 0.01971
Time = 0.01972
Time = 0.01902
Time = 0.01960
Time = 0.02023
Time = 0.01985
Time = 0.01940
Time = 0.01938
Time = 0.01929
Time = 0.01934
Time = 0.02038
Time = 0.01969
Time = 0.01966
Time = 0.01999
Time = 0.01903
Time = 0.01878
Time = 0.01864
Time = 0.01858
Time = 0.01873
Time = 0.01864
Time = 0.01982
Time = 0.02041
Time = 0.02020
Time = 0.01991
Time = 0.01969
Time = 0.01930
Time = 0.02067
Time = 0.01864
Time = 0.01870
Time = 0.01858
Time = 0.01854
Time = 0.01877
Time = 0.01874
Time = 0.01879
Time = 0.01951
Time = 0.01914
Time = 0.01859
Time = 0.01878
Time = 0.01882
Time = 0.01897
Time = 0.01894
Time = 0.01886
Time = 0.01877
Time = 0.01880
Time = 0.01996
Time = 0.01941
Time = 0.02082
Time = 0.01956
Time = 0.01976
Time = 0.02011
Time = 0.02063
Time = 0.02057
Time = 0.02114
Time = 0.02090
Time = 0.02057
Time = 0.02114
Time = 0.02047
Time = 0.02016
Time = 0.01984
Time = 0.02037
Time = 0.01990
Time = 0.01983
Time = 0.01992
Time = 0.01979
Time = 0.01990
Time = 0.02005
Time = 0.02291
Time = 0.02016
Time = 0.01988
Time = 0.01934
Time = 0.02004
Time = 0.02047
Time = 0.01989
Time = 0.01949
Time = 0.01993
Time = 0.01986
Time = 0.01984
Time = 0.02061
Time = 0.02041
Time = 0.01940
Time = 0.01963
Time = 0.01983
Time = 0.02057
Time = 0.01992
Time = 0.01979
Time = 0.02024
Time = 0.02042
Time = 0.01959
Time = 0.02091
Time = 0.02043
Time = 0.01936
Time = 0.02061
Time = 0.02027
Time = 0.01973
Time = 0.02026
Time = 0.02002
Time = 0.02045
Time = 0.02065
Time = 0.02147
Time = 0.02326
Time = 0.02081
Time = 0.01952
Time = 0.01950
Time = 0.01973
Time = 0.01946
Time = 0.01905
Time = 0.01908
Time = 0.01898
Time = 0.01885
Time = 0.02021
Time = 0.01974
Time = 0.01884
Time = 0.01912
Time = 0.01903
Time = 0.01891
Time = 0.01869
Time = 0.01870
Time = 0.01898
Time = 0.01923
Time = 0.01976
Time = 0.01993
Time = 0.02126
Time = 0.01940
Time = 0.01871
Time = 0.01884
Time = 0.01976
Time = 0.01903
Time = 0.01950
Time = 0.02013
Time = 0.02137
Time = 0.01987
Time = 0.02132
Time = 0.02137
Time = 0.02020
Time = 0.01999
Time = 0.01984
Time = 0.02001
Time = 0.01991
Time = 0.01971
Time = 0.01941
Time = 0.01927
Time = 0.01921
Time = 0.02036
Time = 0.01947
Time = 0.02330
Time = 0.02128
Time = 0.01931
Time = 0.01933
Time = 0.01943
Time = 0.01926
Time = 0.01873
Time = 0.01838
Time = 0.01982
Time = 0.02018
Time = 0.02304
Time = 0.01954
Time = 0.02036
Time = 0.02011
Time = 0.02098
Time = 0.02000
Time = 0.01978
Time = 0.01986
Time = 0.01985
Time = 0.01933
Time = 0.01938
Time = 0.01985
Time = 0.01911
Time = 0.01903
Time = 0.01866
Time = 0.01869
Time = 0.01864
Time = 0.01823
Time = 0.01800
Time = 0.01869
Time = 0.01895
Time = 0.01939
Time = 0.02077
Time = 0.02055
Time = 0.02015
Time = 0.02033
Time = 0.01992
Time = 0.02054
Time = 0.02069
Time = 0.02018
Time = 0.01974
Time = 0.01958
Time = 0.02049
Time = 0.01968
Time = 0.01900
Time = 0.01877
Time = 0.01877
Time = 0.01864
Time = 0.01874
Time = 0.01867
Time = 0.01873
Time = 0.01865
Time = 0.01866
Time = 0.02061
Time = 0.01811
Time = 0.01843
Time = 0.01828
Time = 0.01790
Time = 0.01866
Time = 0.01919
Time = 0.01834
Time = 0.01868
Time = 0.01856
Time = 0.01906
Time = 0.02017
Time = 0.01874
Time = 0.01900
Time = 0.02005
Time = 0.01996
Time = 0.02046
Time = 0.01965
Time = 0.01982
Time = 0.01992
Time = 0.01987
Time = 0.02086
Time = 0.02065
Time = 0.02032
Time = 0.01984
Time = 0.01875
Time = 0.01901
Time = 0.01891
Time = 0.01864
Time = 0.01878
Time = 0.01865
Time = 0.01795
Time = 0.01888
Time = 0.01878
Time = 0.02013
Time = 0.01830
Time = 0.01841
Time = 0.01827
Time = 0.01838
Time = 0.01878
Time = 0.01846
Time = 0.01855
Time = 0.01813
Time = 0.01853
Time = 0.01962
Time = 0.01880
Time = 0.01875
Time = 0.01843
Time = 0.01838
Time = 0.01832
Time = 0.01825
Time = 0.01937
Time = 0.01863
Time = 0.01827
Time = 0.01854
Time = 0.02109
Time = 0.01802
Time = 0.01851
Time = 0.01824
Time = 0.01844
Time = 0.01826
Time = 0.01824
Time = 0.01843
Time = 0.01809
Time = 0.01797
Time = 0.01881
Time = 0.02046
Time = 0.01902
Time = 0.01805
Time = 0.01865
Time = 0.01941
Time = 0.01927
Time = 0.01857
Time = 0.01829
Time = 0.01819
Time = 0.01814
Time = 0.01883
Time = 0.01975
Time = 0.01891
Time = 0.01857
Time = 0.01905
Time = 0.01896
Time = 0.01996
Time = 0.01972
Time = 0.01990
Time = 0.02033
Time = 0.01989
Time = 0.02049
Time = 0.02044
Time = 0.01968
Time = 0.01952
Time = 0.02012
Time = 0.01994
Time = 0.01981
Time = 0.02021
Time = 0.01945
Time = 0.01893
Time = 0.01866
Time = 0.01917
Time = 0.02036
Time = 0.01903
Time = 0.01875
Time = 0.01873
Time = 0.01981
Time = 0.02000
Time = 0.01965
Time = 0.02033
Time = 0.02049
Time = 0.02084
Time = 0.02115
Time = 0.01988
Time = 0.02119
Time = 0.02112
Time = 0.02162
Time = 0.02072
Time = 0.02068
Time = 0.02021
Time = 0.02030
Time = 0.01996
Time = 0.02107
Time = 0.02016
Time = 0.02025
Time = 0.02014
Time = 0.01973
Time = 0.02053
Time = 0.01970
Time = 0.02017
Time = 0.01975
Time = 0.02028
Time = 0.02168
Time = 0.02023
Time = 0.02056
Time = 0.02027
Time = 0.02003
Time = 0.02079
Time = 0.02083
Time = 0.02047
Time = 0.02084
Time = 0.02161
Time = 0.02206
Time = 0.02152
Time = 0.02159
Time = 0.02172
Time = 0.02074
Time = 0.02073
Time = 0.02056
Time = 0.02086
Time = 0.02003
Time = 0.02119
Time = 0.02092
Time = 0.02055
Time = 0.02015
Time = 0.02066
Time = 0.01995
Time = 0.02016
Time = 0.02117
Time = 0.02004
Time = 0.02038
Time = 0.01980
Time = 0.02046
Time = 0.01966
Time = 0.02030
Time = 0.02061
Time = 0.02000
Time = 0.02007
Time = 0.02010
Time = 0.01996
Time = 0.01993
Time = 0.02046
Time = 0.01974
Time = 0.02069
Time = 0.02523
Time = 0.02296
Time = 0.02199
Time = 0.02106
Time = 0.01980
Time = 0.01939
Time = 0.01917
Time = 0.01938
Time = 0.01923
Time = 0.01961
Time = 0.02023
Time = 0.01974
Time = 0.02177
Time = 0.02066
Time = 0.01921
Time = 0.02060
Time = 0.01990
Time = 0.01955
Time = 0.01949
Time = 0.01938
Time = 0.01925
Time = 0.01962
Time = 0.01921
Time = 0.01952
Time = 0.01906
Time = 0.01947
Time = 0.01933
Time = 0.01950
Time = 0.01951
Time = 0.02000
Time = 0.01951
Time = 0.02060
Time = 0.02025
Time = 0.01956
Time = 0.01947
Time = 0.01887
Time = 0.01906
Time = 0.01932
Time = 0.01940
Time = 0.01905
Time = 0.01935
Time = 0.01956
Time = 0.02109
Time = 0.01921
Time = 0.01866
Time = 0.01944
Time = 0.01945
Time = 0.01889
Time = 0.01901
Time = 0.01927
Time = 0.01952
Time = 0.01922
Time = 0.01984
Time = 0.02058
Time = 0.01922
Time = 0.01902
Time = 0.01889
Time = 0.01869
Time = 0.01935
Time = 0.01953
Time = 0.01945
Time = 0.01868
Time = 0.01812
Time = 0.02090
Time = 0.01943
Time = 0.02072
Time = 0.02509
Time = 0.02497
Time = 0.02209
Time = 0.01857
Time = 0.01831
Time = 0.01832
Time = 0.01792
Time = 0.01874
Time = 0.01963
Time = 0.02031
Time = 0.01823
Time = 0.01825
Time = 0.01865
Time = 0.01869
Time = 0.01828
Time = 0.01824
Time = 0.01821
Time = 0.01788
Time = 0.01838
Time = 0.01862
Time = 0.02016
Time = 0.01851
Time = 0.01826
Time = 0.01820
Time = 0.01830
Time = 0.01829
Time = 0.01832
Time = 0.01874
Time = 0.01805
Time = 0.01794
Time = 0.02195
Time = 0.01921
Time = 0.01906
Time = 0.01893
Time = 0.01879
Time = 0.01922
Time = 0.01940
Time = 0.01873
Time = 0.01866
Time = 0.01849
Time = 0.01851
Time = 0.01968
Time = 0.01941
Time = 0.01983
Time = 0.01865
Time = 0.01854
Time = 0.01858
Time = 0.01799
Time = 0.01786
Time = 0.01820
Time = 0.01803
Time = 0.01786
Time = 0.01799
Time = 0.02011
Time = 0.01872
Time = 0.01874
Time = 0.01799
Time = 0.01797
Time = 0.01796
Time = 0.01867
Time = 0.02014
Time = 0.01933
Time = 0.01862
Time = 0.01852
Time = 0.01942
Time = 0.01829
Time = 0.02050
Time = 0.01859
Time = 0.01839
Time = 0.01829
Time = 0.01833
Time = 0.01832
Time = 0.01824
Time = 0.01830
Time = 0.01822
Time = 0.01869
Time = 0.02106
Time = 0.01898
Time = 0.01887
Time = 0.01851
Time = 0.01877
Time = 0.01905
Time = 0.01829
Time = 0.01838
Time = 0.01822
Time = 0.01821
Time = 0.01905
Time = 0.01979
Time = 0.01872
Time = 0.01893
Time = 0.01832
Time = 0.02000
Time = 0.02116
Time = 0.01941
Time = 0.02048
Time = 0.01958
Time = 0.02003
Time = 0.02142
Time = 0.02048
Time = 0.01993
Time = 0.02025
Time = 0.02088
Time = 0.01993
Time = 0.01919
Time = 0.01957
Time = 0.01977
Time = 0.01926
Time = 0.01935
Time = 0.01888
Time = 0.01961
Time = 0.01898
Time = 0.01837
Time = 0.01857
Time = 0.01829
Time = 0.01822
Time = 0.01825
Time = 0.01821
Time = 0.01820
Time = 0.01893
Time = 0.01847
Time = 0.02067
Time = 0.02088
Time = 0.01898
Time = 0.01868
Time = 0.01870
Time = 0.01947
Time = 0.01879
Time = 0.01838
Time = 0.01852
Time = 0.01835
Time = 0.01867
Time = 0.01930
Time = 0.01918
Time = 0.01905
Time = 0.01955
Time = 0.01916
Time = 0.02016
Time = 0.01972
Time = 0.01969
Time = 0.01961
Time = 0.01928
Time = 0.01957
Time = 0.02007
Time = 0.01996
Time = 0.02007
Time = 0.01929
Time = 0.01932
Time = 0.01913
Time = 0.01876
Time = 0.01831
Time = 0.01857
Time = 0.01820
Time = 0.02068
Time = 0.01874
Time = 0.01905
Time = 0.01831
Time = 0.01840
Time = 0.01836
Time = 0.01810
Time = 0.01796
Time = 0.01801
Time = 0.01797
Time = 0.01800
Time = 0.01804
Time = 0.02086
Time = 0.01822
Time = 0.01882
Time = 0.01802
Time = 0.01951
Time = 0.01860
Time = 0.01828
Time = 0.01839
Time = 0.01837
Time = 0.01826
Time = 0.01809
Time = 0.01820
Time = 0.01875
Time = 0.02053
Time = 0.01880
Time = 0.01865
Time = 0.01879
Time = 0.01894
Time = 0.01879
Time = 0.01883
Time = 0.01897
Time = 0.01804
Time = 0.01825
Time = 0.02076
Time = 0.01919
Time = 0.01843
Time = 0.01831
Time = 0.01835
Time = 0.01839
Time = 0.01829
Time = 0.01856
Time = 0.01860
Time = 0.01789
Time = 0.01809
Time = 0.01825
Time = 0.01930
Time = 0.02054
Time = 0.01831
Time = 0.01858
Time = 0.01901
Time = 0.01836
Time = 0.01857
Time = 0.01820
Time = 0.01804
Time = 0.01870
Time = 0.01833
Time = 0.01955
Time = 0.02267
Time = 0.01964
Time = 0.02008
Time = 0.01967
Time = 0.02059
Time = 0.02027
Time = 0.02110
Time = 0.01972
Time = 0.02015
Time = 0.02021
Time = 0.02064
Time = 0.01946
Time = 0.01932
Time = 0.01910
Time = 0.01923
Time = 0.01915
Time = 0.01879
Time = 0.01897
Time = 0.01885
Time = 0.01911
Time = 0.01926
Time = 0.02104
Time = 0.01911
Time = 0.01865
Time = 0.01837
Time = 0.01855
Time = 0.01869
Time = 0.01840
Time = 0.01846
Time = 0.01855
Time = 0.01905
Time = 0.01923
Time = 0.02031
Time = 0.01848
Time = 0.01872
Time = 0.01899
Time = 0.01907
Time = 0.01890
Time = 0.01895
Time = 0.01879
Time = 0.01882
Time = 0.01882
Time = 0.01914
Time = 0.02119
Time = 0.01918
Time = 0.01982
Time = 0.01896
Time = 0.01881
Time = 0.01891
Time = 0.01888
Time = 0.01879
Time = 0.01915
Time = 0.01885
Time = 0.01908
Time = 0.02006
Time = 0.01917
Time = 0.01853
Time = 0.01834
Time = 0.01843
Time = 0.01848
Time = 0.01843
Time = 0.01848
Time = 0.01849
Time = 0.01859
Time = 0.01975
Time = 0.02207
Time = 0.01919
Time = 0.01875
Time = 0.01866
Time = 0.01898
Time = 0.01912
Time = 0.01883
Time = 0.01880
Time = 0.01869
Time = 0.01842
Time = 0.01907
Time = 0.02077
Time = 0.01874
Time = 0.01871
Time = 0.01875
Time = 0.01897
Time = 0.01889
Time = 0.01880
Time = 0.01883
Time = 0.01834
Time = 0.01871
Time = 0.01982
Time = 0.02085
Time = 0.01860
Time = 0.01904
Time = 0.01903
Time = 0.01896
Time = 0.01899
Time = 0.01939
Time = 0.01914
Time = 0.02005
Time = 0.02012
Time = 0.02029
Time = 0.01974
Time = 0.01934
Time = 0.01943
Time = 0.01962
Time = 0.01909
Time = 0.01876
Time = 0.01891
Time = 0.01844
Time = 0.01847
Time = 0.01852
Time = 0.01913
Time = 0.01843
Time = 0.02039
Time = 0.01874
Time = 0.01859
Time = 0.01852
Time = 0.01840
Time = 0.01908
Time = 0.01976
Time = 0.01880
Time = 0.01846
Time = 0.01921
Time = 0.02031
Time = 0.01866
Time = 0.01883
Time = 0.01849
Time = 0.01842
Time = 0.01859
Time = 0.01881
Time = 0.01886
Time = 0.01887
Time = 0.01884
Time = 0.01897
Time = 0.02034
Time = 0.02012
Time = 0.01969
Time = 0.01897
Time = 0.01854
Time = 0.01859
Time = 0.01899
Time = 0.01898
Time = 0.01902
Time = 0.01901
Time = 0.01955
Time = 0.02078
Time = 0.01859
Time = 0.01877
Time = 0.01833
Time = 0.01903
Time = 0.02056
Time = 0.01857
Time = 0.01864
Time = 0.01890
Time = 0.01846
Time = 0.02180
Time = 0.01984
Time = 0.01946
19.4 ms ± 469 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)