Re: MSDN volatile sample

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Alex,


Your sample is great. I have stuied your optimization code and I think the
optimization approach you are using to optimize while loop to for loop is a
general solution, not specific code for the special sample on MSDN.

If yes, I think such type of optimization is not safe if the control
condition if while loop -- in this sample is variable Sentinel may be changed
to other value by other threads. Any comments?


regards,
George

"Alex Blekhman" wrote:

"George" wrote:
Wny code like this,

if (Sentinel)
for (;;)
Sleep(0);

is optimized compared with original code,

while (Sentinel)
Sleep(0);

I do not understand why the former is faster?

Because first case generates less instructions. It looks something
like this in pseudocode:

// original code

if (Sentinel)
for (;;)
Sleep(0);

// generated assembly code

load value from memory to register
if not zero jump to STARTFOR address
STARTFOR:
push parameter on stack
call Sleep
jump to STARTFOR address

Now, this code:

while (Sentinel)
Sleep(0);

will generate following instructions:

STARTWHILE:
load value from memory to register
if zero jump to ENDWHILE address
push parameter on stack
call Sleep
jump to STARTWHILE address
ENDWHILE:
...

As you can see in first case costly memory access is factored out
of for-loop's body. In the second case the memory where `Sentinel'
variable is resided is accessed every cycle of while-loop.

HTH
Alex



.



Relevant Pages

  • Re: Why is C# 450% slower than C++ on nested loops ??
    ... The posted benchmark was crucial to ... > compilers generate for the loop and get over with it. ... > additions in the outer loops, which the C# compiler doesn't. ... gotten around to implementing every possible optimization in every language, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Duffs Device
    ... I'd disagree a little on the above assessment that even in C unless you were looking at a very low-level case similar to what Glen was talking about would the optimizer not likely make such improvements as were appropriate for the specific hardware platform/compiler implementation if one simply wrote the "straightahead" loop. ... If, of course, there's a lot of indirection and all inside, that could limit optimization but for the simple case don't think it would help much for most modern compilers. ...
    (comp.lang.fortran)
  • Re: Moving bytes in memory
    ... and over again, albeit correctly, that Premature Optimization ... loop similar to that you are describing! ... of an paraphrase teeming with undeclared variables ... ...
    (comp.lang.c)
  • Re: Why No Supplemental Characters In Character Literals?
    ... "Micro-optimization" is the attempt to optimize tiny portions of the ... instructions in the innermost loop of some arithmetic procedure, ... actual optimization and was never anything other than a condemnatory ... It stands to reason that this, or something close, would be ...
    (comp.lang.java.programmer)
  • Re: JVM vs my VM
    ... The only overhead compared to new allocation within the loop is the allocation itself - the initialization would need to be done in either case. ... Just because the possibility for this optimization exists, and HotSpot is documented to do this sort of thing, doesn't mean that it will happen for a given scenario. ...
    (comp.lang.java.programmer)