What I mean, to measure performance in some algorithm I need to do it in terms of some unit. So, basically if I ignore declarations and definitions of objects and consider only operations , how would you estimate the time each operation consums in run time?.
Share
In
c++11you have thestd::chronolibrary (see std::chrono). Otherwise, you can useboost::chrono(see boost::chrono). Those 2 are very basic time measurement libraries you can start with.EDIT: In c++0x you have the
clock()function – but as noted in the comment section – it is not a very accurate measurement. Plus, most platforms also supply some sort of API for chrono-related operations.How to measure performance depends on the context. First, you’ll want to compare performance on a large enough and diverse enough sample data. Large enough means that it is at least measurable in seconds. Diverse enough means that it covers repetition of all run scenarios either uniformly or perhaps separately (you should at lease know what cut the sample data represents).
For simple enough cases, the first thing you’ll want to do is partition your algorithm into as many as needed segments, let’s call them
A_1,...,A_N(or perhapsA_1...A_Nare different solutions to the same problem, the concept still holds). What you’ll want to do is measure the amount of time spent on each partition over the same sample data. In pseudo-code:At the end of this run you are left with a times vector which shows you how much time you’ve spent in each element. That is the most basic approach. But the key element to profiling is: there are many ways to dissect a frog. You could also look at how much time you are spending on a specific operation which could be called from many partitions of the algorithm. You could choose to look at the number of load/store operations, cache performance etc. The variations are numerous and great reward can often come from unexpected sources (at least to the unwary observer).
For more advanced profiling, you can use a 3rd party profiling framework (some IDEs come with built-in profiling capabilities).