FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
July 02, 2007

Simulating Polymorphic Operators in C++

(Page 5 of 5)

How Do They Compare?

My primary motivation for exploring alternative versions of polymorphic insertion functions was to create a version that uses less memory than the virtual method version while obtaining comparable performance.

To compare the three versions of code, I experimented with six test cases; see Table 1. The first two test cases use objects and not pointers, so they are nonpolymorphic.

Test 1                  
Employee employee1;   
cout << employee1;

Test 2
Manager m1;
cout << manager1;

Test 3
Employee* pEmp = new Employee;
cout << *pEmp;

Test 4
Manager* pManager1 = new Manager;
cout << *pManager1;

Test 5
Employee* pEmp = new Manager;
cout << *pEmp;

Test 6
Employee* pEmp = new Salesperson;
cout << *pEmp;

Table 1: Test cases.

The first test calls the Employee's insertion function using an Employee object. Likewise, the second test calls the Manager's function using a Manager object. The next two test cases use pointers where the type of the pointer matches the type of the object that is being pointed to by the pointer. The third uses an Employee pointer that is pointing to an Employee object. The fourth test uses a Manager pointer that is pointing to a Manager object. The last two test cases assess the polymorphic behavior of the functions. They both use an Employee pointer that points to a Manager and Salesperson object, respectively.

Figure 2 shows the test timing data for each version. The time unit is microseconds. These numbers are the average of several runs using Visual C++ Version 7.

[Click image to view at full size]

Figure 2: Test timing data for each version.

I expected version 2 to be slower because it uses runtime type information (RTTI), but was surprised that it was so much slower. Keep in mind, version 2 is using just as much memory as version 1.

As I hoped, versions 1 and 3 are comparable in performance. In fact, version 3 is a little faster for the first and third test cases. Both of these test cases deal with parent objects. However, version 3 is a little slower for the test cases dealing with child objects.

Version 1 requires a class to have a vtable, plus every object must contain a pointer to the vtable. Version 3 requires each object to have a type identifier, but this needs less memory than the pointer for a vtable. So, version 3 uses less memory per object and per class.

Conclusion

In this article I presented three different techniques for polymorphic insertion functions. The third version is a viable option for situations with a small class hierarchy where more parent objects are used than children. In these examples, I assumed that there would be many more Employee objects than Manager and Salesperson objects.

Previous Page | 1 Simulating Polymorphic Operators in C++ | 2 Version 1: Auxiliary Methods | 3 Version 2: The dynamic_cast Operator | 4 Version 3: An Enumerated Type | 5 How Do They Compare?
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK