Chris@1256: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@1256: 
Chris@1256: /*
Chris@1256:     Sonic Visualiser
Chris@1256:     An audio file viewer and annotation editor.
Chris@1256:     Centre for Digital Music, Queen Mary, University of London.
Chris@1256:     
Chris@1256:     This program is free software; you can redistribute it and/or
Chris@1256:     modify it under the terms of the GNU General Public License as
Chris@1256:     published by the Free Software Foundation; either version 2 of the
Chris@1256:     License, or (at your option) any later version.  See the file
Chris@1256:     COPYING included with this distribution for more information.
Chris@1256: */
Chris@1256: 
Chris@1256: #ifndef HIT_COUNT_H
Chris@1256: #define HIT_COUNT_H
Chris@1256: 
Chris@1256: #include <string>
Chris@1256: #include <iostream>
Chris@1256: 
Chris@1256: /**
Chris@1256:  * Profile class for counting cache hits and the like.
Chris@1256:  */
Chris@1256: class HitCount
Chris@1256: {
Chris@1256: public:
Chris@1256:     HitCount(std::string name) :
Chris@1256: 	m_name(name),
Chris@1256: 	m_hit(0),
Chris@1256: 	m_partial(0),
Chris@1256: 	m_miss(0)
Chris@1256:     { }
Chris@1256:     
Chris@1256:     ~HitCount() {
Chris@1274: #ifndef NO_HIT_COUNTS
Chris@1256: 	using namespace std;
Chris@1256: 	int total = m_hit + m_partial + m_miss;
Chris@1256: 	cerr << "Hit count: " << m_name << ": ";
Chris@1256: 	if (m_partial > 0) {
Chris@1256: 	    cerr << m_hit << " hits, " << m_partial << " partial, "
Chris@1256: 		 << m_miss << " misses";
Chris@1256: 	} else {
Chris@1256: 	    cerr << m_hit << " hits, " << m_miss << " misses";
Chris@1256: 	}
Chris@1256: 	if (total > 0) {
Chris@1256: 	    if (m_partial > 0) {
Chris@1256: 		cerr << " (" << ((m_hit * 100.0) / total) << "%, "
Chris@1256: 		     << ((m_partial * 100.0) / total) << "%, "
Chris@1256: 		     << ((m_miss * 100.0) / total) << "%)";
Chris@1256: 	    } else {
Chris@1256: 		cerr << " (" << ((m_hit * 100.0) / total) << "%, "
Chris@1256: 		     << ((m_miss * 100.0) / total) << "%)";
Chris@1256: 	    }
Chris@1256: 	}
Chris@1256: 	cerr << endl;
Chris@1274: #endif
Chris@1256:     }
Chris@1256: 
Chris@1256:     void hit() { ++m_hit; }
Chris@1256:     void partial() { ++m_partial; }
Chris@1256:     void miss() { ++m_miss; }
Chris@1256: 
Chris@1256: private:
Chris@1256:     std::string m_name;
Chris@1256:     int m_hit;
Chris@1256:     int m_partial;
Chris@1256:     int m_miss;
Chris@1256: };
Chris@1256: 
Chris@1256: #endif