tag:blogger.com,1999:blog-1746745451277571533.post1411434054107883008..comments2024-02-17T12:36:21.608+05:30Comments on Computers, Technology, and Anything in Between: Which "for loop" works better/fasterosgeekhttp://www.blogger.com/profile/12045857609474816436noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-1746745451277571533.post-57870386240497346152007-07-06T23:59:00.000+05:302007-07-06T23:59:00.000+05:30Simple. The first one. Why?Think about how the pro...Simple. The first one. Why?<BR/><BR/>Think about how the program runs. As others have pointed out per the thing of the operator= and branch predictions. But there is a simpler method to evaluate it.<BR/><BR/>Think of how often the the sets of loops will need to run. Where are you going to spend 80% of your time?<BR/><BR/>Since the 'j' loop runs 100 times, and the 'i' loop 10 times; the 'j' loop is the bigger loop to make efficient.<BR/><BR/>Think about it:<BR/><BR/>Loop #1:<BR/>i run from 0 to 9, and resets J, which runs from 0 to 99.<BR/><BR/>Loop #2:<BR/>j run from 0 to 99, and resets i, which runs 0 to 9.<BR/><BR/>Loop #2 will reset the variables many more times than Loop #1. Thus, Loop #1 is the better method. Coincidentally, this will also make your branch-predictions better for the CPU, and give you the optimizations for the operator= as other suggested.TemporalBeinghttps://www.blogger.com/profile/06247647473502902350noreply@blogger.comtag:blogger.com,1999:blog-1746745451277571533.post-90288945135930458532007-06-18T07:39:00.000+05:302007-06-18T07:39:00.000+05:30Gut feeling says #1 because branch prediction will...Gut feeling says #1 because branch prediction will make a mistake only 1/10th as often.<BR/><BR/>However, the difference is negligible because execution time would be dominated by the I/O.Curtishttps://www.blogger.com/profile/03296306438150510559noreply@blogger.comtag:blogger.com,1999:blog-1746745451277571533.post-76718115674980528122007-06-16T20:07:00.000+05:302007-06-16T20:07:00.000+05:30OK, visually loops are same, but if one counts the...OK, visually loops are same, but if one counts the total number of <EM>operator=</EM> executed in first loop...Tairhttps://www.blogger.com/profile/18064604575919112377noreply@blogger.com