Monday 15 June 2015

c# - Performance hit when Ldloca instruction exists in method -



c# - Performance hit when Ldloca instruction exists in method -

i ran unusual behavior, , hoping shed lite on issue:

class sample { public static void main() { const int count = 10000000; while (true) { // move allocation here allocate more more 50x slowdown problem var stopwatch = stopwatch.startnew(); var total = 0; (int = 1; <= count; ++i) { total += i; } console.write("{0}, ", stopwatch.elapsedmilliseconds); if (total > total + 1) console.writeline(((object)total).tostring()); } } }

note cast object in lastly line (which incidentally never run). without it, code takes pretty important performance nail - on macbook pro 15" 2012 running windows 7 .net 4.5 in release, adds 16ms overhead, while running time without overhead when total variable int 3ms, or 11ms if it's double.

the difference can see in ildasm seems added ldloca instruction - existence of instruction affects memory layout somehow?

c# .net performance optimization cil

No comments:

Post a Comment