11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

392Part VI: The Part of TensFigure 30-1:A profileanalysisshows youwhere aprogram isspendingits time.Interpreting a profile takes a certain amount of practice. This window showsthe functions invoked during the execution of the program (there may be otherfunctions in the program, but they were never called). The first column lists thenames of the function followed by the percentage of time spent in that functionin the second column. In this case, just more than 24 percent of the program’sexecution time was spent in the copy constructor Person::Person(Person&).The Self Secs column refers to the total amount of time spent within the function— an entire 0.14 second was spent in the copy constructor (almost onefifthof a second — shocking!).Does this mean that the copy constructor is the slowest function in the program?Not necessarily. In reality, the program spent more time in this functionbecause it was called more often than any other — the copy constructoris invoked from both fn1() and fn2().Skipping down to these two functions, you can see that fn2() took more timethan fn1(). In fact, fn2() took twice as much time as fn1() — 0.04 secondversus 0.02 second. fn1() creates a new copy of the Person object passed toit. However, fn1() receives its argument by reference from main().By comparison, main() passes the Person object to fn2() by value. Thiscauses C++ to invoke the copy constructor. The fn2() function then makes acopy of the copy. Finally, fn2() creates the copy from heap memory using thenew keyword. Allocating memory off the heap takes a certain amount of time.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!