{"id":901,"date":"2019-02-16T17:13:35","date_gmt":"2019-02-16T08:13:35","guid":{"rendered":"http:\/\/141.164.34.82\/?p=901"},"modified":"2022-02-03T17:27:28","modified_gmt":"2022-02-03T08:27:28","slug":"speed-comparsion-for-apply-and-colsum","status":"publish","type":"post","link":"http:\/\/ds.sumeun.org\/?p=901","title":{"rendered":"Speed Comparsion : for, apply, and colSum"},"content":{"rendered":"<h1>R\uc758 \ubc18\ubcf5\ubb38\uc758 \uc18d\ub3c4 \ube44\uad50<\/h1>\n<p>\ub3d9\uc77c\ud55c \ubc18\ubcf5 \uc791\uc5c5\uc744 \uc218\ud589\ud558\ub294 \uc5ec\ub7ec \uac00\uc9c0 \ub2e4\ub978 \ubc29\ubc95\uc758 \uc18d\ub3c4\ub97c \ube44\uad50\ud574 \ubcf4\uba74 \uc18d\ub3c4\uc758 \uc21c\uc11c\ub294 \ub300\ubd80\ubd84 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<blockquote><p>R\uc758 \ub0b4\uc7a5 \ud568\uc218 &gt; <code>apply<\/code> &gt; <code>for<\/code><\/p><\/blockquote>\n<pre><code class=\"r\">mat &lt;- matrix(1:1000, 1000, 1000, byrow=T)\r\n# R \ub0b4\uc7a5 \ud568\uc218\r\nresult &lt;- colSums(mat)\r\n\r\n# apply\r\nresult &lt;- apply(mat, 2, sum)\r\n\r\n# for\r\nresult &lt;- rep(NA, 1000) # result &lt;- NA, length(result) &lt;- 1000\r\nfor (i in 1:ncol(mat)) {\r\n  result[i] &lt;- sum(mat[,i])\r\n}\r\n<\/code><\/pre>\n<p>R\uc758 \uacfc\uac70 \ubc84\uc804\uc5d0\uc11c\ub294 <code>for<\/code>\ubb38\uc744 \uc4f0\ub294 \uac83\ubcf4\ub2e4 <code>apply<\/code>\uc744 \uc4f0\ub294 \uac83\uc774 \uc555\ub3c4\uc801\uc73c\ub85c \ube68\ub790\ub2e4\uace0 \ud55c\ub2e4. \ud558\uc9c0\ub9cc Hadley\uac00 \uc9c0\uc801\ud588\ub4ef\uc774 \ucd5c\uadfc \ubc84\uc804\uc758 R\uc5d0\uc11c\ub294 <code>for<\/code>\ubb38\uc744 \uc0ac\uc6a9\ud574\ub3c4 \uc131\ub2a5 \uc800\ud558\uac00 \uadf8\ub9ac \ud06c\uc9c0 \uc54a\ub2e4.<\/p>\n<p>\uc5ec\uae30\uc11c \uc7a0\uae50 \uc2e4\ud5d8\uc744 \ud574\ubcf4\uc790. \uc544\ub798\uc758 \ucf54\ub4dc\ub85c \ub3d9\uc77c\ud55c \uae30\ub2a5(\ud589\ub82c\uc758 \uac01 \uc5f4\uc744 \ud569\ud55c\ub2e4)\uc744 \ud558\ub294 \uc11c\ub85c \ub2e4\ub978 \ubc29\ubc95\uc5d0 \ub300\ud55c \uc18c\uc694 \uc2dc\uac04\uc744 \uce21\uc815\ud574 \ubcf4\uba74 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre><code class=\"r\">mat &lt;- matrix(1:1000, 1000, 1000, byrow=T)\r\nresult &lt;- vector(mode=\"list\", 1000)\r\n# R \ub0b4\uc7a5 \ud568\uc218\r\ntColsum &lt;- system.time({for (i in 1:1000)\r\n  result[[i]] &lt;- colSums(mat)})\r\n\r\nmat &lt;- matrix(1:1000, 1000, 1000, byrow=T)\r\nresult &lt;- vector(mode=\"list\", 1000)\r\n# for\r\ntFor &lt;- system.time(for(iter in 1:1000) {\r\n  result[[iter]] &lt;- rep(NA, 1000) # result &lt;- NA, length(result) &lt;- 1000\r\n  for (i in 1:ncol(mat)) {\r\n    result[[iter]][i] &lt;- sum(mat[,i]) \r\n  }})\r\n\r\nmat &lt;- matrix(1:1000, 1000, 1000, byrow=T)\r\nresult &lt;- vector(mode=\"list\", 1000)\r\n# apply\r\ntApply &lt;- system.time({for (i in 1:1000)\r\n  result[[i]] &lt;- apply(mat, 2, sum)\r\n})\r\n<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th align=\"center\"><\/th>\n<th align=\"center\"><code>colSums<\/code><\/th>\n<th align=\"center\"><code>apply(  , 2, sum)<\/code><\/th>\n<th align=\"center\"><code>for<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">\uc18c\uc694 \uc2dc\uac04(\ucd08)<\/td>\n<td align=\"center\">0.87<\/td>\n<td align=\"center\">7.75<\/td>\n<td align=\"center\">5.05<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uc704\uc758 \uacb0\uacfc\ub97c \ubcf4\uba74 \uc18d\ub3c4 \uc21c\uc11c\ub294 \uccab \ubc88\uc9f8\uac00 R\uc5d0\uc11c \uadf8 \uae30\ub2a5\ub9cc\uc744 \uc704\ud574 \ud2b9\ubcc4\ud788 \uc81c\uc791\ub41c \ud568\uc218, \ub450 \ubc88\uc9f8\uac00 <code>for<\/code>, \uc138 \ubc88\uc9f8\uac00 <code>apply<\/code>\uc774\ub2e4. \uc774 \uacbd\uc6b0\uc5d0\ub294 \uc704\uc5d0\uc11c \uc598\uae30\ud55c \uc77c\ubc18\uc801\uc778 \uacbd\uc6b0\uc640 \ub2e4\uc18c \ud070 \ucc28\uc774\ub97c \ubcf4\uc778\ub2e4. \ub530\ub77c\uc11c \uc18d\ub3c4\uac00 \uc911\uc694\ud55c \uacbd\uc6b0\uc5d0\ub294 \uc0c1\ud669\uacfc \uc870\uac74\uc5d0 \ub9de\ucdb0 \ubaa8\uc758 \uc2e4\ud5d8\uc744 \ud574\ubcf4\ub294 \uac83\uc774 \uc88b\uc744 \uac83\uc774\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>R\uc758 \ubc18\ubcf5\ubb38\uc758 \uc18d\ub3c4 \ube44\uad50 \ub3d9\uc77c\ud55c \ubc18\ubcf5 \uc791\uc5c5\uc744 \uc218\ud589\ud558\ub294 \uc5ec\ub7ec \uac00\uc9c0 \ub2e4\ub978 \ubc29\ubc95\uc758 \uc18d\ub3c4\ub97c \ube44\uad50\ud574 \ubcf4\uba74 \uc18d\ub3c4\uc758 \uc21c\uc11c\ub294 \ub300\ubd80\ubd84 \ub2e4\uc74c\uacfc \uac19\ub2e4. R\uc758 \ub0b4\uc7a5 \ud568\uc218 &gt; apply &gt; for mat &lt;- matrix(1:1000, 1000, 1000, byrow=T) # R \ub0b4\uc7a5 \ud568\uc218 result &lt;- colSums(mat) # apply result &lt;- apply(mat, 2, sum) # for result &lt;- rep(NA, 1000) # result [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2466,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,151],"tags":[39,154,153,152],"jetpack_featured_media_url":"http:\/\/ds.sumeun.org\/wp-content\/uploads\/2019\/02\/oranges-g291001788_640.jpg","_links":{"self":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/901"}],"collection":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=901"}],"version-history":[{"count":3,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/901\/revisions"}],"predecessor-version":[{"id":2467,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/901\/revisions\/2467"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/media\/2466"}],"wp:attachment":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=901"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}