Re: MSDN volatile sample
- From: George <George@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 29 Dec 2007 05:47:00 -0800
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
- Follow-Ups:
- Re: MSDN volatile sample
- From: Alex Blekhman
- Re: MSDN volatile sample
- References:
- Re: MSDN volatile sample
- From: Norbert Unterberg
- Re: MSDN volatile sample
- From: Norbert Unterberg
- Re: MSDN volatile sample
- From: George
- Re: MSDN volatile sample
- From: ajk
- Re: MSDN volatile sample
- From: George
- Re: MSDN volatile sample
- From: Alex Blekhman
- Re: MSDN volatile sample
- From: George
- Re: MSDN volatile sample
- From: Alex Blekhman
- Re: MSDN volatile sample
- From: George
- Re: MSDN volatile sample
- From: Alex Blekhman
- Re: MSDN volatile sample
- From: George
- Re: MSDN volatile sample
- From: Alex Blekhman
- Re: MSDN volatile sample
- Prev by Date: Re: MSDN volatile sample
- Next by Date: Re: MSDN volatile sample
- Previous by thread: Re: MSDN volatile sample
- Next by thread: Re: MSDN volatile sample
- Index(es):
Relevant Pages
|