Advertisement

How good is the C++Compiler?

Started by May 03, 2000 06:20 AM
9 comments, last by The big Question? 24 years, 7 months ago
Well I have been wondering a bit about, how good/optimized the C++Compiler is. If I write code like:

a = 23 +7;
 
Will the compiler optimize this code to:

a = 30;
 
Which is faster and always correct. Also when you write C++ code like:

func()
{
  UINT  u1, u2, u3;

  // Do something.
  UINT  u4, u5;

}

 
Is the compiler then allocating memory 2 places, or just in the begining of the function. Because if the memory gets allocated one place, there´s a bigger chance, that it will fit into the CPUs cache, which is faster. Hope somebody knows this, because I want fast code! But don´t we all?
<<>> The Big ? <<>>
Depends on the compiler, but there''s an easy way to find out: compile that code, disassemble, and see what it has generated!


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
I could do that, but I don´t know assembler that well.
I can read some of it, but I still have to learn more, before getting skilled enough.

Also the source for a Windows program in assembler is very big , so I can´t find the correct line to look in.
Perhaps there is an easy breakpoint kind-of-way?

So have anyone tried this, and knows the answer to my question?

Edited by - The big Question? on 5/3/00 6:39:16 AM
<<>> The Big ? <<>>
quote:
If I write code like:
a = 23 +7;  


Will the compiler optimize this code to:
a = 30;  


Everything I have read suggests that yes, it does optimize. This is one of the reasons #defines are so fast.

Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
Most optimizing compilers do both of the ones you mention. You can check your compiler manual for a list of the optional optimizations.

Some optimizations, like the ones you mention, are "no brainers" and aren''t even mentioned in the manual, because there''s no chance that they can break "fragile" code.

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

If you want to see the assembly code for a specific block of instructions, set a breakpoint there and then when you get to the breakpoint, open up the disassambly window to look at the assembler instructions. (Well atleast using msvc++, but I assume other compilers have similar features.)
Advertisement
Thanks to all who helped...
Now I can test the compiler, and hopefully write better optimized code.


Edited by - The big Question? on 5/4/00 5:35:39 PM
<<>> The Big ? <<>>
> Also the source for a Windows program in assembler is
> very big , so I can´t find the correct line to look in.
> Perhaps there is an easy breakpoint kind-of-way?

There''s another way (I prefer):
- In your c source add comment like //FINDTHIS
- Let the compiler generate a listing file.
- View the listing file and search for FINDTHIS.

*** It''s also possible to include your C source as comment in the listing file.

Hope this helps,
Bas.
quote: Original post by baskuenen

There''s another way (I prefer):
- In your c source add comment like //FINDTHIS
- Let the compiler generate a listing file.
- View the listing file and search for FINDTHIS .



Hi there!
I am pretty sure that I have read somewhere, that the compiler edits out all the comments when compiling, which makes sense, because if if was left there, it would only be a waste of space(in compiled code anyway)...
If the comments were to stay in compiled code, people would be relctant to even include comments in their code, as it would increase the size of thier .exe''s

davior





Big difference between compiling to assembly and compiling to object code. Of course when compiled to object code comments are stripped; however, when compiled to assembly some compilers retain comments. Not to mention sticking in some comments of their own, such as original C source for the assembly block.

And allocating the memory at the beginning of the function isn''t so much an optimization, as it is part of the calling convention.

This topic is closed to new replies.

Advertisement