Measuring Performance...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Measuring Performance...

 
0
  #1
Aug 11th, 2007
Hi, i 've recently done a university project requiring me to construct a data structure{ a heap}....

Now that i 've finished the programming task , i am thinking how can i measure the programm's performance.....

I will populate my data structure with 2 million random elements, and i will measure the time it takes to complete the varius operations on it....

The question is how can i do this...????

PS:: after the project gets marked i will post it online.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Measuring Performance...

 
0
  #2
Aug 11th, 2007
Which OS / Compiler / type of machine ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Measuring Performance...

 
0
  #3
Aug 11th, 2007
use clock() to get the time before starting the algorithm and again afterwards, then subtract the two times. For example
  1. clock_t start = clock();
  2. // do something, such as call a function
  3. clock_t end = clock();
  4. clock_t diff = end - start;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Measuring Performance...

 
0
  #4
Aug 11th, 2007
  1. #include <algorithm>
  2. #include <vector>
  3. #include <iostream>
  4. #include <ctime>
  5. #include <iterator>
  6. #include <boost/random.hpp>
  7. #include <limits>
  8. using namespace std ;
  9. using namespace boost ;
  10.  
  11. inline double elapsed_msecs( clock_t start, clock_t end )
  12. { return double( end - start ) * 1000.0 / CLOCKS_PER_SEC ; }
  13.  
  14. int main()
  15. {
  16. // choose a reasonably good random number generator
  17. // and a distribution that mirrors the intended use of the heap
  18. mt19937 engine ; // mersene twister; fast with acceptable quality.
  19. uniform_int<> distribution( 1, 1024*1024 ) ; // just as an example
  20. variate_generator< mt19937&, uniform_int<> >
  21. generator( engine, distribution ) ;
  22.  
  23. // measure performance for a your implementation and
  24. // a reference implementation. this example measures
  25. // performance of the reference implementation in libstdc++
  26. // for two operations: make_heap O(N) and sort_heap O(N log N)
  27. enum { HEAP_SIZE = 2*1024*1024, ITERATIONS = 32 };
  28. double make_heap_total = 0.0 ;
  29. double make_heap_min = numeric_limits<double>::max() ;
  30. double make_heap_max = 0.0 ;
  31. double sort_heap_total = 0.0 ;
  32. double sort_heap_min = numeric_limits<double>::max() ;
  33. double sort_heap_max = 0.0 ;
  34.  
  35. // run the test several times
  36. for( int i=0 ; i<ITERATIONS ; ++i )
  37. {
  38. vector<int> vec ; vec.reserve(HEAP_SIZE) ;
  39. generate_n( back_inserter(vec), size_t(HEAP_SIZE), generator ) ;
  40.  
  41. clock_t start = clock() ;
  42. make_heap( vec.begin(), vec.end() ) ;
  43. clock_t end = clock() ;
  44. double duration = elapsed_msecs( start, end ) ;
  45. make_heap_total += duration ;
  46. make_heap_min = min( make_heap_min, duration ) ;
  47. make_heap_max = max( make_heap_max, duration ) ;
  48.  
  49. start = clock() ;
  50. sort_heap( vec.begin(), vec.end() ) ;
  51. end = clock() ;
  52. duration = elapsed_msecs( start, end ) ;
  53. sort_heap_total += duration ;
  54. sort_heap_min = min( sort_heap_min, duration ) ;
  55. sort_heap_max = max( sort_heap_max, duration ) ;
  56. }
  57.  
  58. cout << "resolution of libc clock: " << 1000.0 / CLOCKS_PER_SEC << " millisecs\n" ;
  59. cout << "make_heap - min: " << make_heap_min << " max: " << make_heap_max
  60. << " avj: " << make_heap_total / ITERATIONS << " millisecs\n" ;
  61. cout << "sort_heap - min: " << sort_heap_min << " max: " << sort_heap_max
  62. << " avj: " << sort_heap_total / ITERATIONS << " millisecs\n" ;
  63. }
  64.  
  65. // compile with optimizations enabled and test
  66. /*
  67. >g++ -Wall -std=c++98 -O3 -march=pentium4 -I/usr/local/include perf_metric.cpp && ./a.out
  68. resolution of libc clock: 7.8125 millisecs
  69. make_heap - min: 54.6875 max: 62.5 avj: 60.0586 millisecs
  70. sort_heap - min: 1328.12 max: 1335.94 avj: 1334.23 millisecs
  71. */
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Measuring Performance...

 
0
  #5
Aug 11th, 2007
i was concerned that if i used clock, it's output would compromised by the fact that it is run from inside the program....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Measuring Performance...

 
0
  #6
Aug 11th, 2007
I suppose you could use an external program to time it, but it would have a problem too in that it takes some time for one program to spawn another program so you would not be gaining anything. Calling the clock() function executes very quickly and would not throw off the timings very much if you execute the algorithm numerious times (such as a thousand times or so) before executing the clock() function again.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Measuring Performance...

 
0
  #7
Aug 12th, 2007
the time taken to call clock() would be much lower than the resolution of the clock provided by the library.
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std ;
  4.  
  5. int main()
  6. {
  7. const double res = 1000.0 / CLOCKS_PER_SEC ;
  8. cout << "resolution of libc clock: " << res << " millisecs\n" ;
  9. int ncalls = 0 ;
  10. enum { NTESTS = 1024 } ;
  11. for( int i=0 ; i<NTESTS ; ++i )
  12. {
  13. clock_t t1 = clock(), t2 = t1+1 ;
  14. while( clock() == t1 ) ;
  15. while( clock() == t2 ) ++ncalls ;
  16. }
  17. cout << "time taken to call clock: " << NTESTS * res / ncalls << " millisecs\n" ;
  18. }
  19. /*
  20. >g++ -O3 -march=pentium4 -Wall -std=c++98 clock_time.cpp && ./a.out
  21. resolution of libc clock: 7.8125 millisecs
  22. time taken to call clock: 0.00506795 millisecs
  23. */
since the clock ticks are at discrete intervals, there can be an error of upto one clock tick in a single measurement. for example,
|--------|S+++++++|++++++++|++++++++|++++++++|++++++++|+++++++E|--------|--------| 5 ticks
|--------|-------S|++++++++|++++++++|++++++++|++++++++|++++++++|+++++E--|--------| 6 ticks
we can clearly make this out in the difference between the min and max times posted earlier. to minimize the statistical expectation of error
a. the time measured should be an order of magnitude higher than the resolution of the clock.
b. run the test several times; the average should then give a better indication
the time taken to call clock one or two times would be negigible in comparison.

if you are attempting to profile your code for identifying and removing performance bottlenecks, you should use a standard profiler. a good profiler would (approximately) adjust for the profiling overhead while reporting time information.
Last edited by vijayan121; Aug 12th, 2007 at 12:47 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Measuring Performance...

 
0
  #8
Aug 12th, 2007
guys your posts are very helpful {specially with vijayan121's posts i try to follow...}

Some more questions:

1st) where can i find: mt19937

2nd) i just want to test a structure for a university project.... can a profiler help me?If yes, can you suggest me an easy-to-learn one....

3rd) except from the time taken is it possible to see how much space is consumed?
{because i have the following incident testing with 10 million elements it finishes the job rather fast, but when i go to 50 million elements the sytem stops repsonding... i guess it isn't a matter of computer power, but of ram space....

Thank you all for your help
Last edited by n.aggel; Aug 12th, 2007 at 6:53 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Measuring Performance...

 
0
  #9
Aug 12th, 2007
Yet my request for basic information about your setup goes unheeded - oh well, enjoy.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Measuring Performance...

 
0
  #10
Aug 12th, 2007
n.aggel> where can i find: mt19937
the one i have used is from the boost libraries. http://www.boost.org/index.htm

n.aggel> ... can a profiler help me? If yes, can you suggest me an easy-to-learn one....
n.aggel> .. is it possible to see how much space is consumed? ...
Salem>> Which OS / Compiler / type of machine?
if you had given that information, Salem would have given you an answer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC