Hi.
I'm programming in C++ using Visual Studio.
For efficiency purposes, I tried using the goto statement (I have a
while-loop with 3 breaking conditions - cond1&&cond2&&cond3, and after
finishing the loop, I want to know the reason for getting out).
This currently shows a nice overall performance improvement of 14%.
What I'd like to know, is whether using goto might confuse the compiler
and make it avoid doing certain optimizations. Do you know if that's
possible, or maybe just the opposite?
Thanks,
bytesgrip.
|
|
0
|
|
|
|
Reply
|
spamtrap2 (1628)
|
3/9/2005 9:18:01 PM |
|
bytesgrip,
Even though it is long out of fashion, there are places where using a
GOTO simply does the job better. The compiler will probably optimise
the code you have written without any problems. What you can try and do
is help it along some by reducing the number of jumps in the code as
they do have a performance penalty on any late hardware.
Regards,
hutch at movsd dot com
|
|
0
|
|
|
|
Reply
|
hutch
|
3/10/2005 5:45:47 AM
|
|
bytesgrip wrote:
> Hi.
>
> I'm programming in C++ using Visual Studio.
>
> For efficiency purposes, I tried using the goto statement (I have a
> while-loop with 3 breaking conditions - cond1&&cond2&&cond3, and after
> finishing the loop, I want to know the reason for getting out).
Normally Visual C++ will detect this exact scenario and branch where you
want it to go. I wrote a strange little test program to demonstrate this.
The I am only using the /Ox compiler option.
int globalvar1 = 0;
int globalvar2 = 0;
int globalvar3 = 0;
int foo(void)
{
while(globalvar1 == 0 && globalvar2 == 0)
{
if (globalvar3 == 0)
break;
}
if (globalvar1 != 0)
return 0;
if (globalvar2 != 0)
return 1;
return 2;
}
PUBLIC ?foo@@YAHXZ ; foo
; Function compile flags: /Ogty
; File c:\documents and settings\mtaylor\desktop\foo.cpp
_TEXT SEGMENT
?foo@@YAHXZ PROC NEAR ; foo
mov eax, DWORD PTR ?globalvar1@@3HA ; globalvar1
test eax, eax
jne SHORT $L290
mov eax, DWORD PTR ?globalvar2@@3HA ; globalvar2
mov ecx, DWORD PTR ?globalvar3@@3HA ; globalvar3
$L275:
test eax, eax
jne SHORT $L291
test ecx, ecx
jne SHORT $L275
mov eax, 2
ret 0
$L290:
xor eax, eax
ret 0
$L291:
mov eax, 1
ret 0
?foo@@YAHXZ ENDP ; foo
> This currently shows a nice overall performance improvement of 14%.
>
> What I'd like to know, is whether using goto might confuse the
> compiler and make it avoid doing certain optimizations. Do you know
> if that's possible, or maybe just the opposite?
It seems to me that goto would create a ton of problems for the analyzer,
but I haven't tested it specifically. Loops and if statements are much
easier to optimize because you can predict the state of registers through
all paths. That is not true with goto statements. You know the state of the
registers, but it would be harder to arrange for things to be in the right
places.
Anyway, I am curious as to how you pull 14% performance improvement doing
something the compiler already does. Which compiler flags are you using?
-Matt
|
|
0
|
|
|
|
Reply
|
Matt
|
3/10/2005 6:10:48 AM
|
|
"bytesgrip" <spamtrap@crayne.org> wrote:
>
>I'm programming in C++ using Visual Studio.
>
>For efficiency purposes, I tried using the goto statement (I have a
>while-loop with 3 breaking conditions - cond1&&cond2&&cond3, and after
>finishing the loop, I want to know the reason for getting out).
>
>This currently shows a nice overall performance improvement of 14%.
>
>What I'd like to know, is whether using goto might confuse the compiler
>and make it avoid doing certain optimizations. Do you know if that's
>possible, or maybe just the opposite?
gotos do break the compiler's "train of thought", so to speak. The
compiler tries to build a nice, neat flow graph out of your application,
and it uses that flow graph to adjust its assumptions about registers and
temporaries. Undisciplined use of gotos force the compiler to abandon that
knowledge, and can cause a lot of reloading of temporaries.
However, the "goto as shortcut exit from loop" is a well-understood and
commonly used idiom. The compiler will be able to recognize that and treat
it as a "break".
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
|
|
0
|
|
|
|
Reply
|
Tim
|
3/10/2005 6:27:42 AM
|
|
hi
Regarding compiler optimization techniques for loops , It finally converts
any loop into do -while loop . And do-while loop is implemented using jump
instruction in assembly.
Regarding ur doubt , Using goto stmt in ur program will affect readability
but its behaviour is same.
Mohan
"bytesgrip" <spamtrap@crayne.org> wrote in message
news:1110400479.335197.63810@z14g2000cwz.googlegroups.com...
> Hi.
>
> I'm programming in C++ using Visual Studio.
>
> For efficiency purposes, I tried using the goto statement (I have a
> while-loop with 3 breaking conditions - cond1&&cond2&&cond3, and after
> finishing the loop, I want to know the reason for getting out).
>
> This currently shows a nice overall performance improvement of 14%.
>
> What I'd like to know, is whether using goto might confuse the compiler
> and make it avoid doing certain optimizations. Do you know if that's
> possible, or maybe just the opposite?
>
> Thanks,
> bytesgrip.
>
|
|
0
|
|
|
|
Reply
|
invincible
|
3/23/2005 4:59:47 AM
|
|
|
4 Replies
414 Views
(page loaded in 0.071 seconds)
|