Escolar Documentos
Profissional Documentos
Cultura Documentos
Comparing C/C++
Compilers
It’s all about flexibility, portability, efficiency, and performance
Matthew Wilson
D
espite the advent of new programming languages and tech- 1. C1. A large (1000 functions) monolithic (no include files) C-
nologies, C++ is the workhorse for many developers, and file (compilation only; no optimizations).
is likely to remain so for a long time to come. The main 2. C2. A C file with a large number (500) of include files (com-
reasons for C++’s prominence are its flexibility, portabili- pilation only; no optimizations).
ty, efficiency, and performance. Yes, even with the increase in 3. C3. A C file with a large number (100) of nested include files,
processing power, software performance continues to be im- each of which is included by its prior file, and then by the
portant, and C++ is a language that — when used correctly— main file, thereby testing the effects of multiple inclusions
provides superior performance in virtually any context. and include guards (compilation only; no optimizations).
In this article, I compare nine popular C++ compilers in terms 4. pch. A suite of C++ files (main.cpp, pch.cpp, and 40 .h/.cpp
of performance, features, and tools. The compilers are either ex- class files) sharing common header(s), facilitating precom-
clusively Win32 or provide Win32 variants. I conducted all stud- piled headers (compile and link; precompiled headers; no
ies on a Windows XP Pro machine (single-processor, 2 GHz, 512 optimizations).
MB) with no other busy processes. The compilers I examine are: 5. whereis. A single complex C++ file with several template
and operating-system library includes (compilation only; op-
• Borland C/C++ 5.6 (C++ Builder 6). http://www.borland timized for space). This tool provides powerful command-
.com/products/downloads/download_cbuilder.html line searching and is included as a sample in the STLSoft li-
• Digital Mars C/C++ 8.34. http://www.digitalmars.com/ braries, exercising much STLSoft code.
• GNU C/C++ 3.2 (The MinGW 2.0 distribution). http://www 6. MMComBsc. A large (44 C and 37 C++ source files, 111 head-
.gnu.org/software/gcc/ er files, 80 KB in production) DLL providing COM functions
• Intel C/C++ 7.0. http://intel.com/ and classes (compile and link; precompiled headers; opti-
• Metrowerks CodeWarrior 8.3. http://store.metrowerks.com/ mized for space).
• Microsoft Visual C++ 6.0. http://shop.microsoft.com/devtools/ 7. zlib. A free, general-purpose, data-compression library portable
• Microsoft Visual C++.NET 2002 (VC++ 7.0). across hardware and operating-system platforms.
• Microsoft Visual C++.NET 2003 (VC++ 7.1).
• Watcom C/C++ 12.0 (Open Watcom C/C++ 1.0). http:// I used Python scripts (available electronically; see “Resource
openwatcom.org/ Center,” page 5) to generate the source files for scenarios 1– 4. The
source files are very large and not included with this article. The
As for bias, I confess to having soft spots for DigitalMars, In- whereis source is available at http://stlsoft.org/. (You can get the
tel, and CodeWarrior, all of which have helped me in creating most up-to-date binary from my company’s web site, http://
the STLSoft libraries (http://stlsoft.org/). Nevertheless, my day- synesis.com.au/r_systools.html.) The source files for MMComBsc.dll
to-day tool of choice is not one of these. contain too many proprietary goodies for me to include here, so
you’ll have to take my word for the figures reported.
Compilation Time I used ptime (http://synesis.com.au/r_systools.html) to get the
In many situations, compilation time is not important. However, it results from scenarios 1–3 and 5 by executing multiple (15) times,
is crucial on large systems or in development situations with fre- discarding the two-highest and one-lowest results, and report-
quent builds (such as Extreme Programming). When compiling/link- ing an average of the rest. This reduces distortion from caching
ing source, important factors include the number of inclusions, or startup. I executed scenarios 4, 6, and 7 using makefiles, tim-
use of precompiled headers, complexity of code, aggressiveness ing the process via ptime. Table 1 presents the results.
of optimization (in both compilation and linking), and size of trans- The “Did Not Compile” (DNC) notation for CodeWarrior in
lation units. For this article, I considered these scenarios: scenario C3 results from the compiler refusing to process the
nested include depth of 100; tests showed that 30 was the lim-
Matthew is a consultant for Synesis Software, as well as au- it. CodeWarrior help says, “To fix this error, study the logic be-
thor of the STLSoft libraries and the upcoming Imperfect C++ hind your nested #includes. There’s probably a way of dividing
(Addison-Wesley, 2004). He can be contacted at matthew@synesis the large nested #includes into a series of smaller nests”— which
.com.au or http://stlsoft.org/. is probably true, but may not always be so. Watcom could not
Table 1: Compilation times (in ms) of the seven scenarios for the range of compilers. (Figures in bold are best results.)
Dhrystones/s
Dhrystone 1,847,675 1,605,696 2,993,718 2,496,510 2,606,319 2,490,718 2,263,404 2,450,608 484,132
time (ms)
Int2string a (sprintf()) 7642 5704 5808 7714 7933 9419 7802 7813 5539
Int2string b (STLSoft) 3140 1289 3207 1679 1156 1624 1808 1843 DNC
StringTok a (Boost) 4746 3272 DNC 6809 1450 2705 2641 2341 DNC
StringTok b (STLSoft) 636 809 280 385 382 579 383 406 DNC
RectArr (1 iteration) 1082 910 997 1590 859 915 824 887 DNC
RectArr (10 iterations) 6922 3168 5589 3853 1649 1995 1533 1828 DNC
zlib (small) 92 110 88 92 87 87 91 78 90
zlib (large) 8412 12,550 8847 11,310 9390 10,875 10,266 9117 15,984
Table 2: Execution times (in ms) of the nine scenarios for the range of compilers. (Figures in bold are best results.)
The Digital Mars is the only compiler that does not support
them, although it will do so from Version 8.35 onwards. Note
that neither Borland 5.5(1) or 5.6 are able to optimize them out
of code, leading to performance costs.
Variable-length arrays (VLAs) and dynamic application of the
sizeof operator are C99 features. Only Digital Mars and GCC
support them. Except for VC++ 6, all compilers support covari-
ant return types.
Koenig lookup is a useful mechanism (see my article “Gen-
eralized String Manipulation: Access Shims and Type Tunnel-
ing,” C/C++ Users Journal, August 2003), whereby operations
associated with an element from one namespace may be auto-
matically accessed from another without namespace qualifica-
tion. VC++ (6 and 7), Watcom, and, except with nondefault pa-
rameter, Intel, do not support this.
The for-scoping rules were changed in C++.98 (ISO/IEC C++
Standard, 1998), and all compilers except VC++ 6 and Wat-
com support the new syntax. Interestingly, Intel gives a warn-
ing when used correctly, but in a way that would fail to com-
pile under the old rule. (In my opinion, you should never
write code that relies on either old or new rules, so this should
not occur in production code. This warning is useful to avoid
doing so.)
All the remaining issues involve templates. Though it does
have some template support, Watcom fails on all remaining tests.
VC++ 6 and 7 fail on the important facility of partial specializa-
tion, although Version 7.1 provides full support.
One VC++ 6 weirdness is that it won’t accept the typename
qualifier within the default parameters of a template, something
other compilers (GCC and CodeWarrior, for example) mandate.
You must resort to the preprocessor to support them all. (Con-
formance masochists can check out the definition of ss_type-
name_type_def_k in the STLSoft headers.)
Except for Digital Mars, VC++ 6.0, and Watcom, all compil-
ers support template templates. This technique isn’t currently
widely used, but is useful and will be more so in the future. Fu-
ture compilers need to support it.
Overall, GCC is the clear winner. Since I think static asser-
tions, 80-bit floating-point, and Koenig lookup are more im-
portant than VLAs and _ _ func_ _, I would put Borland sec-
ond, CodeWarrior and VC++ 7.1 third, Digital Mars fourth, and
Intel fifth. I would rate all of these as good compilers. Next
come the other VC++s and Watcom. Once the next version of
Digital Mars is released, it will likely have a perfect score, too.
However, you can expect likewise from other vendors soon,
since language conformance has become a marketable fea-
ture once more.
One feature not included in Table 4 involves typedef templates
(see “The New C++: Typedef Templates,” by Herb Sutter, C/C++
Users Journal, December 2002), which none of the compilers
15%
Faster Integer Performance
Performance!
1084 Outstanding performance on Intel architecture including Intel®
1000
939 Pentium® 4, Intel® Xeon™ and Intel Itanium® 2 processors,
800
as well as support for Hyper-Threading Technology.
600
Compatibility
400
Windows
200 • Plugs into Microsoft Visual Studio* (6.0 and .NET*)
0 • Native source and binary compatibility with MS Visual C++*
Leading C++ Intel C++
for Windows Compiler 7.0 • Strong compatibility with Compaq Visual Fortran*
SPECint_base 2000† Linux
†
Intel® Pentium 4 Processor, 3.05 GHz, 512 KB • Source/binary compatibility with GNU C, and extensive
L2 Cache, 256MB Memory, Windows XP
Professional MP Kernel, Build 2600 source/binary compatibility with gcc 3.2 for C++.
31%
Faster Integer
Support
Performance! 1034
1000
Purchase includes 1 year of free product upgrades and
789
Intel Premier Support
800
600 “We recently switched to the Intel C++ Compiler from a leading compiler for
400 use on Maya 5.0’s dynamics code. The output ran so much faster—up to 90%
faster—than before, that our engineer didn’t actually believe it and rechecked
200
his data. To say the least, we are impressed with the Intel product.”
0
GCC 3.2 Intel C++ —Kevin Tureski
Compiler 7.0 General Manager of Maya Engineering
SPECint_base 2000†† Alias|Wavefront
††
Intel® Pentium 4 Processor, 3.05 GHz, 512 KB
L2 Cache, 256MB Memory, Red Hat* Linux* 8.0
Dhrystone 60,416 57,344 53,788 98,118 49,152 45,056 45,056 45,056 47,616
Int2string a (sprintf()) 56,320 57,344 40,476 95,282 40,960 40,960 36,864 40,960 33,792
Int2string b (STLSoft) 56,320 57,344 40,476 95,953 40,960 40,960 36,384 40,960 DNC
StringTok a (Boost) 78,336 69,632 DNC 458,004 65,536 57,344 49,152 53,248 DNC
StringTok b (STLSoft) 68,096 65,536 43,036 453,945 53,248 53,248 45,056 49,152 DNC
RectArr 64,512 65,536 43,548 100,343 31,744 45,056 40,960 45,056 DNC
zlib (small) 75,776 86,016 83,484 151,916 65,536 61,440 53,248 57,344 81,408
MMComBsc 214,528 245,760 138,780 DNC 98,304 90,112 102,400 102,400 DNC
Table 3: Size of modules (in bytes) of the eight scenarios for the range of compilers. (Figures in bold are best results.)
Feature Borland CodeWarrior Digital Mars GCC Intel VC++ 6 VC++ 7 VC++ 7.1 Watcom
800-445-7899 • programmersparadise.com
(continued from page 22) about fourth on execution size.) For now, it is let down by
aren’t going to pry many programmers from their favorite en- nonstandard Standard Library support, the occasional ICE, and
vironments. an outdated IDDE— but it’s free and you get such good ser-
Perhaps anachronistically, my editor of choice is the Visual vice from its developer that these things are forgivable.
Studio 97 IDDE, which I use because I know the keystrokes, can • GCC has the best language support, which is commensurate
write some useful wizards/plug-ins/macros, and it’s quick, doesn’t with its having the widest collaboration of any open-source
require the use of the mouse, can debug, and doesn’t crash. I (and probably commercial as well) compiler in the business.
have experience with the IDDEs of C++ Builder, Digital Mars, However, this may also account for its poor efficiency char-
CodeWarrior, and Visual Studio 98, and .NET, and they either acteristics. On my tested scenarios, it proved to be the slow-
have too many or too few features, crash, or make me take my est compiler, and produced the slowest and fattest code. (This
hands off the keyboard. Nonetheless, I know people who swear can be contrasted with Digital Mars, which is written and main-
by them, so it’s a case of to each his own. tained by one person.)
• For speed of generated code — which is often the most im-
Conclusion portant factor — Intel reigns supreme. It does well on the
Clearly, you cannot simply say compiler X is superior to all oth- size of generated code, although it is let down somewhat by
ers. Most compilers are superior to others in one or more respects. compilation speed. It scores well on language issues, al-
though getting it to listen to warning-suppression commands
• Borland is the fastest compiler, has good language support, is sometimes a trial. It does not have any kind of IDDE, but
and doesn’t shame itself in any other regard. Though it’s not plugs into Visual Studio (98 and .NET) without problems,
one of my compilers of choice, it has value to contribute in other than that precompiled headers and browse informa-
its good warnings. However, it seems to share VC++’s predilec- tion go to pot. I’m always surprised when I speak with clients
tion for internal compiler errors (ICEs) when things get too who are writing performance-sensitive software in a Microsoft
hard for it, which I find annoying. environment (Win32 systems, Visual Studio) and yet have
• For me, CodeWarrior is the last word in language rules, con- not considered using the Intel compiler. It’s definitely an es-
formance, and error-message readability— and it is invoked sential part of a programmer’s armory.
whenever another compiler balks at something I’ve written • Visual C++ does better than I expected. It produces the small-
that I think should be okay. Moreover, it has a good (though est code, is quick to compile, and does well in the perfor-
not great) IDDE, produces reasonably efficient code, and has mance of generated code (though it’s still way off Intel’s per-
support for all the popular libraries. It would be the compil- formance). It also, finally, has good language support; VC++
er I’d chose if I could only have one. 7.1 even discovered some typename qualification errors in the
• Digital Mars comes off very well, featuring in the top three for STLSoft libraries that CodeWarrior and GCC missed. And, in
language support, compile time, and execution speed. (It’s my opinion, it has the best IDDE by a country mile. It is/has
been badly let down by poor language support (now im-
pressively, though belatedly, addressed in Version 7.1), long
update cycles (it was five years between Versions 6 and 7),
ridiculous gigabyte install sizes, and by assumptions in the
compiler and the IDDE that you will be using Microsoft ex-
tensions, MFC, and so on. Furthermore, there are too many
ICEs for my liking (even one should be an embarrassment),
which makes you question how much testing goes into it. Fi-
nally, WTL should be a fully supported part of Visual Stu-
dio.NET, wizards and all.
• Watcom is poor on template support, which made it hard to
give it a fair comparison with all the other compilers. It did
well in the areas in which it featured, although Dhrystone per-
formance was extremely poor. I hope it will continue to de-
velop in its Open Watcom guise, and return to the glories that
were Watcom C/C++ in the mid ’90s.
DDJ
Detailed
product
{
information
at your
Product fingertips
categories for
fast searches
Company
information
and more!
Job seekers can search through job listings, find career advice on resume
writing, interviewing and negotiations … employers can search for skilled and
qualified candidates and post current job openings … ALL ON ONE SITE!
VISIT TODAY!
www.developercareers.com