W
Willy Denoyette [MVP]
Rich said:Also that is not the complete code that I tested with. The data was
inserted
into an array and used outside the loop.
Richard
I know, else the loop would be optimized away and the test would finish in 0
msec.

Willy.
Willy Denoyette said:James,
Again, the OP is talking about "unmanaged" C++, the output you show is
managed C++ generated IL....
Willy.
James Curran said:James, the code is not "really dumb code", if you pay attention to the
"n"
in the equations and in my test, the code had scope outside the loop. It
was
not reduced to nothingness. Each line was evaluated several million
times.
I
tried inserting all values into an array which had scope outside the
loop.
Each line was evaluated. The results were the same.
Hey, I looked at the C++ compiler output. You want to see it? Here it
is:
.global ?main@@$$HYAHXZ ; main
?main@@$$HYAHXZ: ; main
; .proc.def D:I()
; Function Header:
; max stack depth = 1
; function size = 2 bytes
; local varsig tk = 0x0
; Exception Information:
; 0 handlers, each consisting of filtered handlers
; .proc.beg
; 12 : double a = 0,b = 0, c = 0, d = 0, e = 0;
; 13 : double total = 0;
; 14 : for(int n = 0; n != 6000000; n++)
; 15 : {
; 16 : a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
; 17 : b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;
; 18 : c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
; 19 : d = n + 1 * 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
; 20 : e = n / 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
; 21 : total += a + b + c + d + e;
; 22 : }
; 23 : // Console::WriteLine(total);
; 24 : return 0;
0000c 16 ldc.i.0 0 ; i32 0x0
0000d 2a ret
.end ?main@@$$HYAHXZ ; main
; .proc.end.i4
text ENDS
END
Look carefully: the only code between the start of main and .end?main
loads
the exit value with zero & returns. It's all of two bytes long: 16 2A. It
doesn't matter that n exists outside the loop --- you only use it to
create
a - e, and you don't use them for anything, so they are unnecessary,
and
are
removed. In fact, in the code I compiled there, I even left in the
counter
"total", which is defined outside the loop. BUT, since I commented out
where I print it out, it became unecessary and was removed. Unless you
read
some value from that array that you put the values into, it would be
unnecessary and be removed.
--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com