{"id":6624,"date":"2019-09-22T15:43:20","date_gmt":"2019-09-22T14:43:20","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=6624"},"modified":"2019-09-22T23:32:11","modified_gmt":"2019-09-22T22:32:11","slug":"when-numpy-empty-is-not-faster-than-numpy-zeros","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=6624","title":{"rendered":"When numpy.empty is not faster than numpy.zeros"},"content":{"rendered":"<p>In many introductions to numpy, one gets taught about <strong>np.ones<\/strong>, <strong>np.zeros<\/strong> and <strong>np.empty<\/strong>. The accepted wisdom is that np.empty will be faster than np.ones because it doesn&#8217;t have to waste time doing all that initialisation. A quick test in a Jupyter notebook shows that this seems to be true!<\/p>\n<pre>import numpy as np\r\n\r\nN = 200\r\nzero_time = %timeit -o some_zeros = np.zeros((N,N))\r\nempty_time = %timeit -o empty_matrix = np.empty((N,N))\r\nprint('np.empty is {0} times faster than np.zeros'.format(zero_time.average\/empty_time.average))\r\n\r\n\r\n8.34 \u00b5s \u00b1 202 ns per loop (mean \u00b1 std. dev. of 7 runs, 100000 loops each)\r\n436 ns \u00b1 10.6 ns per loop (mean \u00b1 std. dev. of 7 runs, 1000000 loops each)\r\nnp.empty is 19.140587654682545 times faster than np.zeros\r\n<\/pre>\n<p>20 times faster may well be useful in production when using really big matrices. Might even be worth the risk of dealing with uninitialised variables even though they are scary!<\/p>\n<p>However&#8230;..on my machine (Windows 10, Microsoft Surface Book 2 with 16Gb RAM), we see the following behaviour with a larger matrix size (1000 x 1000).<\/p>\n<pre>import numpy as np\r\n\r\nN = 1000\r\nzero_time = %timeit -o some_zeros = np.zeros((N,N))\r\nempty_time = %timeit -o empty_matrix = np.empty((N,N))\r\nprint('np.empty is {0} times faster than np.zeros'.format(zero_time.average\/empty_time.average))\r\n\r\n\r\n113 \u00b5s \u00b1 2.97 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10000 loops each)\r\n112 \u00b5s \u00b1 1.01 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10000 loops each)\r\nnp.empty is 1.0094651980894993 times faster than np.zeros\r\n<\/pre>\n<p>The speed-up has vanished! A <a href=\"https:\/\/twitter.com\/walkingrandomly\/status\/1175935052854779904\">subsequent discussion on twitter<\/a> suggests that this is probably because the operating system is zeroing all of the memory for security reasons when using numpy.empty on large arrays.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In many introductions to numpy, one gets taught about np.ones, np.zeros and np.empty. The accepted wisdom is that np.empty will be faster than np.ones because it doesn&#8217;t have to waste time doing all that initialisation. A quick test in a Jupyter notebook shows that this seems to be true! import numpy as np N = [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[31,67],"tags":[],"class_list":["post-6624","post","type-post","status-publish","format-standard","hentry","category-python","category-scientific-software"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1IQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/6624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6624"}],"version-history":[{"count":6,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/6624\/revisions"}],"predecessor-version":[{"id":6631,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/6624\/revisions\/6631"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}