{"id":1169,"date":"2015-06-09T20:03:35","date_gmt":"2015-06-10T01:03:35","guid":{"rendered":"http:\/\/mathewkiang.com\/?p=1169"},"modified":"2020-01-11T16:11:31","modified_gmt":"2020-01-11T21:11:31","slug":"the-colon-operator-really-is-the-fastest","status":"publish","type":"post","link":"https:\/\/mathewkiangcom.local\/2015\/06\/09\/the-colon-operator-really-is-the-fastest\/","title":{"rendered":"The colon operator really is the fastest."},"content":{"rendered":"

\"\"W<\/span>ay back when I was first learning R<\/code>, I ran across an old listserv post that talked about how\u00a0the colon (:<\/code>) operator<\/a>\u00a0was the fastest way to generate a sequence. I never really thought about it, but I got in the habit of always using it whenever I needed a sequence.<\/p>\n

Anecdotally, I knew from running a few simulations that seq()<\/code> should be avoided if you’re generating a lot of small sequences repeatedly, but that’s a relatively rare case. Is the colon operator really that much faster than the alternatives \u2014 seq()<\/code>, seq.int()<\/code>, or seq_len()<\/code> \u2014 in general cases?<\/p>\n

Turns out the answer is “yes” \u2014 most of the time. Running a simple microbenchmark<\/code> script, I tested the generation of numbers from $latex 10^1$ to $latex 10^9$ for each of the four functions. Then I plotted the mean with bars representing the $latex 2.5$th and $latex 97.5$th percentiles (on a log-log plot).<\/p>\n

If you’re generating large sequences, it really doesn’t seem to matter which function you use, but for the common cases (e.g., slicing a vector or enumerating a loop), the colon operator outperforms the others. I’m not really sure there’s a lesson here except to trust R<\/code> listserv posts and use :<\/code> as often as possible. Code here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

ay back when I was first learning R, I ran across an old listserv post that talked about how\u00a0the colon (:) operator\u00a0was the fastest way to generate a sequence. I never really thought about it, but I got in the habit of always using it whenever I needed a sequence.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[132],"tags":[],"_links":{"self":[{"href":"https:\/\/mathewkiangcom.local\/wp-json\/wp\/v2\/posts\/1169"}],"collection":[{"href":"https:\/\/mathewkiangcom.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mathewkiangcom.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mathewkiangcom.local\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mathewkiangcom.local\/wp-json\/wp\/v2\/comments?post=1169"}],"version-history":[{"count":0,"href":"https:\/\/mathewkiangcom.local\/wp-json\/wp\/v2\/posts\/1169\/revisions"}],"wp:attachment":[{"href":"https:\/\/mathewkiangcom.local\/wp-json\/wp\/v2\/media?parent=1169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mathewkiangcom.local\/wp-json\/wp\/v2\/categories?post=1169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mathewkiangcom.local\/wp-json\/wp\/v2\/tags?post=1169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}