{"id":5720,"date":"2016-01-01T23:25:04","date_gmt":"2016-01-01T22:25:04","guid":{"rendered":"http:\/\/emilkirkegaard.dk\/en\/?p=5720"},"modified":"2016-01-01T23:30:25","modified_gmt":"2016-01-01T22:30:25","slug":"r-fastest-way-of-finding-out-of-all-elements-of-a-vector-are-identical","status":"publish","type":"post","link":"https:\/\/emilkirkegaard.dk\/en\/2016\/01\/r-fastest-way-of-finding-out-of-all-elements-of-a-vector-are-identical\/","title":{"rendered":"R: fastest way of finding out of all elements of a vector are identical?"},"content":{"rendered":"<p>There is a question on SO about this: http:\/\/stackoverflow.com\/questions\/4752275\/test-for-equality-among-all-elements-of-a-single-vector<\/p>\n<p>But I was a bit more curious, so!<\/p>\n<pre>#test data, large vectors\r\nv1 = rep(1234, 1e6)\r\nv2 = runif(1e6)\r\n\r\n#functions to try\r\nall_the_same1 = function(x) {\r\n\u00a0 range(x) == 0\r\n}\r\n\r\nall_the_same2 = function(x) {\r\n\u00a0 max(x) == min(x)\r\n}\r\n\r\nall_the_same3 = function(x) {\r\n\u00a0 sd(x) == 0\r\n}\r\n\r\nall_the_same4 = function(x) {\r\n\u00a0 var(x) == 0\r\n}\r\n\r\nall_the_same5 = function(x) {\r\n\u00a0 x = x - mean(x)\r\n\u00a0 all(x == 0)\r\n}\r\n\r\nall_the_same6 = function(x) {\r\n\u00a0 length(unique(x)) == 1\r\n}<\/pre>\n<p>Simple enough, 6 functions to try and some test data. Then we benchmark:<\/p>\n<pre>library(\"microbenchmark\")\r\n\r\nmicrobenchmark(all_the_same1(v1),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same2(v1),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same3(v1),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same4(v1),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same5(v1),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same6(v1))\r\n\r\nmicrobenchmark(all_the_same1(v2),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same2(v2),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same3(v2),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same4(v2),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same5(v2),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all_the_same6(v2))<\/pre>\n<p>Results (for me) look like this, for the first vector:<\/p>\n<pre id=\"rstudio_console_output\" class=\"GEM3DMTCFGB\" tabindex=\"0\">Unit: milliseconds\r\n              expr       min        lq      mean    median        uq       max neval\r\n all_the_same1(v1)  4.321870  4.393697  5.554033  4.442849  4.690950 73.166929   100\r\n all_the_same2(v1)  2.467258  2.494175  2.522648  2.509389  2.534696  2.706289   100\r\n all_the_same3(v1)  3.661536  3.701472  3.783067  3.736434  3.810016  4.496828   100\r\n all_the_same4(v1)  3.657147  3.708786  3.774908  3.746528  3.804603  4.343520   100\r\n all_the_same5(v1)  6.850276  7.029768  8.515351  7.227547  9.164957 73.208182   100\r\n all_the_same6(v1) 15.083830 15.217973 15.977563 15.400977 17.114863 18.679829   100<\/pre>\n<p>And the second:<\/p>\n<pre id=\"rstudio_console_output\" class=\"GEM3DMTCFGB\" tabindex=\"0\">Unit: milliseconds\r\n              expr       min        lq      mean    median        uq       max neval\r\n all_the_same1(v2)  4.304317  4.393111  4.868236  4.487904  4.730886  6.729151   100\r\n all_the_same2(v2)  2.468428  2.498563  2.556797  2.521823  2.570829  3.447666   100\r\n all_the_same3(v2)  3.643104  3.715955  3.822649  3.756476  3.866921  5.887129   100\r\n all_the_same4(v2)  3.634034  3.704983  3.793290  3.765984  3.827717  4.488051   100\r\n all_the_same5(v2)  6.031952  6.206910  9.855640  6.447258  8.357751 80.946411   100\r\n all_the_same6(v2) 67.558621 69.806449 72.405274 71.504097 73.557513 88.433322   100<\/pre>\n<p>In both cases, function #2 is the fastest one. It also does not depend much on the actual data given.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a question on SO about this: http:\/\/stackoverflow.com\/questions\/4752275\/test-for-equality-among-all-elements-of-a-single-vector But I was a bit more curious, so! #test data, large vectors v1 = rep(1234, 1e6) v2 = runif(1e6) #functions to try all_the_same1 = function(x) { \u00a0 range(x) == 0 } all_the_same2 = function(x) { \u00a0 max(x) == min(x) } all_the_same3 = function(x) { \u00a0 sd(x) [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2089],"tags":[1979],"class_list":["post-5720","post","type-post","status-publish","format-standard","hentry","category-programming","tag-r","entry"],"_links":{"self":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/5720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/comments?post=5720"}],"version-history":[{"count":3,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/5720\/revisions"}],"predecessor-version":[{"id":5723,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/5720\/revisions\/5723"}],"wp:attachment":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/media?parent=5720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/categories?post=5720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/tags?post=5720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}