{"id":1629,"date":"2016-09-14T17:09:32","date_gmt":"2016-09-14T17:09:32","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1629"},"modified":"2016-09-14T17:27:49","modified_gmt":"2016-09-14T17:27:49","slug":"why-for-loop-is-expensive","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/why-for-loop-is-expensive\/","title":{"rendered":"Why for Loop is Expensive?"},"content":{"rendered":"<p><strong>Why not use for(int i=0; i< v.size();i++){}?<\/strong><\/p>\n<p><em>For loops are expensive to the processor when the collection reaches large sizes, as many operations are done just to compute the first line:<\/em><\/p>\n<p><strong>For Loop<\/strong><\/p>\n<ol>\n<li>int i = 0 is an assignment and creation (2 operations)<\/li>\n<li>i get size, check value of i, and compare (3 operations)<\/li>\n<li>i++ gets i then adds 1 to it [++i is only 2 operations] this one (3 operations)<\/li>\n<li>7\/8 operations in total, each time the loop runs through<\/li>\n<\/ol>\n<p><strong>While Loop<\/strong><\/p>\n<ol>\n<li>where an enumeration or iterator uses a while(){}<\/li>\n<li>while(v.hasNext()) has next true or false (1 operation)<\/li>\n<li>while(v.hasMoreElements()) has more true or false (1 operation)<\/li>\n<li>Only one operation per repeat of this loop<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Why not use for(int i=0; i< v.size();i++){}? For loops are expensive to the processor when the collection reaches large sizes, as many operations are done just to compute the first line: For Loop int i = 0 is an assignment and creation (2 operations) i get size, check value of i, and compare (3 operations)&hellip; <a href=\"https:\/\/codethataint.com\/blog\/why-for-loop-is-expensive\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[193],"tags":[],"class_list":["post-1629","post","type-post","status-publish","format-standard","hentry","category-interview-questions-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1629"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1629\/revisions"}],"predecessor-version":[{"id":1633,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1629\/revisions\/1633"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}