Online Book Reader

Home Category

Professional C__ - Marc Gregoire [427]

By Root 1467 0
in your output. Because of the efficiency of this second attempt, the timings are getting so small that you might see more map methods in the output than NameDB methods.

On my test laptop, main() now takes only 0.21 seconds: a 67-fold improvement! There are certainly further improvements that you could make on this program. For example, the current constructor performs a lookup to see if the name is already in the map, and if not, adds it to the map. You could combine these two operations. You could always use the insert() method of the map without first checking if the name already exists. The insert() method returns a pair. The Boolean will be true if it added the name to the map, and false if the name was already present. When the name is already in the map, you increment its count, which you can reach through the iterator in the returned pair. This iterator is a map entry, so to access the count, you use res.first->second. To implement this improvement, you can remove the nameExistsAndIncrement() and addNewName() methods, and change the constructor as follows:

NameDB::NameDB(const string& nameFile) throw (invalid_argument)

{

// Open the file and check for errors

ifstream inFile(nameFile.c_str());

if (!inFile) {

throw invalid_argument("Unable to open file");

}

// Read the names one at a time.

string name;

while (inFile >> name) {

auto res = mNames.insert(make_pair(name, 1));

if (res.second == false) {

res.first->second++; // Already in the map, increment count

}

}

inFile.close();

}

Another improvement could be to use caching to cache ranking of names, but this is left as an exercise for the reader.

Profiling Example with Visual C++ 2010

Microsoft Visual C++ 2010 Premium and Ultimate come with a great built-in profiler, which is briefly discussed in this section. The VC++ profiler has a complete graphical user interface. We are not recommending one or the other profiler, but it is always good to have an idea of what a command-line based profiler like gprof can provide in comparison to a GUI-based profiler like the one included with VC++.

Profile of the First Design Attempt

To start profiling an application in Visual C++ 2010, you first need to open the project in Visual Studio. This example uses the same NameDB code as in the first inefficient design attempt earlier. This code is not repeated here. Once your project is opened in Visual Studio, click on the “Analyze” menu and then choose “Profiler ⇒ New Performance Session.” A new docked window should appear. Figure 24-1 shows a screenshot of this window.

FIGURE 24-1

In this new window, click the first button on the toolbar at the top which is the “Launch Performance Wizard” button. This will start a wizard. The first page of this wizard is shown in Figure 24-2.

FIGURE 24-2

The VC++ 2010 profiler has four different kinds of profiling methods:

CPU Sampling: Used to monitor applications with low overhead. This means that the act of profiling the application will not have a big performance impact on the target application.

Instrumentation: This method will add extra code to the application to be able to accurately count the number of function calls and to time individual function calls. However, this method has a much bigger performance impact on the application. It is recommended to use the CPU Sampling method first to get an idea about the bottlenecks in your application. If this method does not give you enough information, you can try the Instrumentation method.

.NET Memory Allocation: This method is not relevant to native C++ applications.

Concurrency: This allows you to monitor multithreaded applications. It allows you to graphically see which threads are running, which threads are waiting for something, and so on.

For this profiling example, leave the default CPU Sampling method and click the “Next” button. The next page of the wizard will ask you to select the application that you want to profile. Here you should select your NameDB project and click the “Next” button. On the last page of the wizard you can enable

Return Main Page Previous Page Next Page

®Online Book Reader