Professional C__ - Marc Gregoire [428]
When the program execution is finished, Visual Studio will automatically open the profiling report. Figure 24-3 shows how this report might look when profiling the first attempt of the NameDB application.
FIGURE 24-3
From this report you can immediately see the hot path. Just like with gprof, it shows that nameExists() and incrementNameCount() are taking up most of the running time of the program, 47.1 and 43.1 percent of the running time on my testing laptop. The Visual Studio profiling report is interactive. For example, you can drill down the nameExists() method by clicking on it. This will result in a drill-down report for that function, which looks like Figure 24-4.
FIGURE 24-4
This drill-down view of the nameExists() method shows a graphical breakdown at the top, and the actual code of the method at the bottom. The code view shows the percentage of the running time that a line needed. The line using up most of the time is shown in red, and the line using up second most of the time is shown in yellow. When you are interactively browsing the profiling report, you can always go back by using the back arrow at the top-left of the report.
At the top of the report there is also a drop-down menu, which you can use to quickly jump to certain summary or details pages. If you go back to the “Summary” of the profiling report, you can see that there is a “Show Trimmed Call Tree” link on the right. Clicking that link will display a trimmed call tree showing you an alternative view of the hot path in your code. This is shown in Figure 24-5.
FIGURE 24-5
Also in this view, you immediately see that main() is calling the NameDB constructor, which is calling nameExists() and incrementNameCount(), which are using up most of the time.
SUMMARY
This chapter discussed the key aspects of efficiency and performance in C++ programs, and provided several specific tips and techniques for designing and writing more efficient applications. We hope you gained an appreciation for the importance of performance and for the power of profiling tools. The most important thing to remember from this chapter is to think about performance and efficiency from the beginning of your program life cycle: Design-level efficiency is far more important than language-level efficiency.
Chapter 25
Developing Cross-Platform and Cross-Language Applications
WHAT’S IN THIS CHAPTER?
How to write code that runs on multiple platforms
How to mix different programming languages together
C++ programs can be compiled to run on a variety of computing platforms and the language has been rigorously defined to ensure that programming in C++ for one platform is very similar to programming in C++ for another. Yet, despite the standardization of the language, platform differences eventually come into play when writing professional-quality programs in C++. Even when development is limited to a particular platform, small differences in compilers can elicit major programming headaches. This chapter examines the necessary complication of programming in a world with multiple platforms and multiple programming languages.
The first part of this chapter surveys the platform-related issues that C++ programmers encounter. A platform is the collection of all of the details that make up your development and/or run-time system. For example, your platform may be the Microsoft Visual C++ 2010 compiler running on Windows 7 on an Intel Core i7 processor. Alternatively,