c# - Efficiency of initializing strings inside or outside of a loop -
in c# console application have loop iterates through 8000 items in collection, setting 8 strings equal various properties of items , using strings perform big variety of other operations. keep code organized i'm declaring of strings ahead of time , leaving them null until item read , loop sets appropriate string equal appropriate value. question is, there noticeable performance difference between declaring strings outside of loop versus inside? know technically eat clock cycles declaring them each time loop iterates, , after 8000 iterations might start add up, have no idea how or if matter? application takes half hour complete full cycle couple seconds of difference insignificant, if we're talking minutes that's bad thing.
please keep in mind question of curiosity, declare these variables wherever without affecting application. i'm sure best practices state should outside loop, i've wondered how of difference makes in kind of low-impact operation.
my question is, there noticeable performance difference between declaring strings outside of loop versus inside?
no there won't. re-initializing string inside loop. if making program more work initializing code outside loop , doing again inside. guess compiler optimize away though.
i know technically eat clock cycles declaring them each time loop iterates
you should let compiler it's job , optimize you. trying compiler's work frustrates , makes more difficult optimize code. complete untrue also.
Comments
Post a Comment