Commit d9ec631b authored by Akarsh Simha's avatar Akarsh Simha
Browse files

Add profiling code to catch bad uses of CachingDms

A bad use of CachingDms would be one where a CachingDms is created or
an angle is changed, but the cached sin/cos values are never made use
of.
parent 62b1e6ac
......@@ -29,7 +29,8 @@
#ifdef COUNT_DMS_SINCOS_CALLS
unsigned long CachingDms::cachingdms_constructor_calls = 0;
unsigned long CachingDms::cachingdms_delta = 0; // difference of ( trig function calls ) - ( trig computations )
long CachingDms::cachingdms_delta = 0; // difference of ( trig function calls ) - ( trig computations )
unsigned long CachingDms::cachingdms_bad_uses = 0;
#endif
CachingDms::CachingDms( const double &x ) : dms( x )
......@@ -38,6 +39,7 @@ CachingDms::CachingDms( const double &x ) : dms( x )
#ifdef COUNT_DMS_SINCOS_CALLS
++cachingdms_constructor_calls;
cachingdms_delta -= 2;
m_cacheUsed = false;
#endif
}
......@@ -47,6 +49,7 @@ CachingDms::CachingDms(const QString& s, bool isDeg) : dms( s, isDeg ) {
#ifdef COUNT_DMS_SINCOS_CALLS
++cachingdms_constructor_calls;
cachingdms_delta -= 2;
m_cacheUsed = false;
#endif
}
......@@ -56,6 +59,7 @@ CachingDms::CachingDms(const int &d, const int &m, const int &s, const int &ms)
#ifdef COUNT_DMS_SINCOS_CALLS
++cachingdms_constructor_calls;
cachingdms_delta -= 2;
m_cacheUsed = false;
#endif
}
......@@ -72,6 +76,11 @@ void CachingDms::setUsing_atan2(const double & y, const double & x) {
m_cos = x/r;
m_sin = y/r;
#ifdef COUNT_DMS_SINCOS_CALLS
if( !m_cacheUsed )
++cachingdms_bad_uses;
m_cacheUsed = false;
#endif
// One may be tempted to do the following:
// dms::setRadians( atan2( y, x ) );
// m_cos = dms::cos();
......@@ -90,6 +99,11 @@ void CachingDms::setUsing_asin(const double & sine) {
// Note: The below is valid because in the range of asin, which is
// [-pi/2, pi/2], cosine is always non-negative
m_cos = std::sqrt( 1 - sine*sine );
#ifdef COUNT_DMS_SINCOS_CALLS
if( !m_cacheUsed )
++cachingdms_bad_uses;
m_cacheUsed = false;
#endif
}
void CachingDms::setUsing_acos(const double & cosine) {
......@@ -98,6 +112,11 @@ void CachingDms::setUsing_acos(const double & cosine) {
// Note: The below is valid because in the range of acos, which is
// [0, pi], sine is always non-negative
m_sin = std::sqrt( 1 - cosine*cosine );
#ifdef COUNT_DMS_SINCOS_CALLS
if( !m_cacheUsed )
++cachingdms_bad_uses;
m_cacheUsed = false;
#endif
}
CachingDms CachingDms::fromString(const QString& s, bool deg) {
......@@ -116,6 +135,7 @@ CachingDms::CachingDms(const dms& angle) {
#ifdef COUNT_DMS_SINCOS_CALLS
++cachingdms_constructor_calls;
cachingdms_delta -= 2;
m_cacheUsed = false;
#endif
}
......
......@@ -38,6 +38,7 @@ public:
*/
CachingDms() : dms(), m_sin( NaN::d ), m_cos( NaN::d ) {
#ifdef COUNT_DMS_SINCOS_CALLS
m_cacheUsed = true;
++cachingdms_constructor_calls;
#endif
};
......@@ -65,6 +66,9 @@ public:
inline void setD( const double &x ) { dms::setD( x ); dms::SinCos( m_sin, m_cos );
#ifdef COUNT_DMS_SINCOS_CALLS
cachingdms_delta -= 2;
if( !m_cacheUsed )
++cachingdms_bad_uses;
m_cacheUsed = false;
#endif
}
......@@ -76,6 +80,9 @@ public:
inline void setH( const double &x ) { dms::setH( x ); dms::SinCos( m_sin, m_cos );
#ifdef COUNT_DMS_SINCOS_CALLS
cachingdms_delta -= 2;
if( !m_cacheUsed )
++cachingdms_bad_uses;
m_cacheUsed = false;
#endif
}
......@@ -96,6 +103,9 @@ public:
inline void setFromString( const QString &s, bool isDeg = true ) { dms::setFromString( s, isDeg ); dms::SinCos( m_sin, m_cos );
#ifdef COUNT_DMS_SINCOS_CALLS
cachingdms_delta -= 2;
if( !m_cacheUsed )
++cachingdms_bad_uses;
m_cacheUsed = false;
#endif
}
......@@ -105,6 +115,9 @@ public:
inline void setRadians( const double &a ) { dms::setRadians( a ); dms::SinCos( m_sin, m_cos );
#ifdef COUNT_DMS_SINCOS_CALLS
cachingdms_delta -= 2;
if( !m_cacheUsed )
++cachingdms_bad_uses;
m_cacheUsed = false;
#endif
}
......@@ -138,6 +151,7 @@ public:
inline void SinCos( double &s, double &c ) const { s = m_sin; c = m_cos;
#ifdef COUNT_DMS_SINCOS_CALLS
cachingdms_delta += 2;
m_cacheUsed = true;
#endif
}
......@@ -149,6 +163,7 @@ public:
inline double sin() const {
#ifdef COUNT_DMS_SINCOS_CALLS
++cachingdms_delta;
m_cacheUsed = true;
#endif
return m_sin; }
......@@ -160,6 +175,7 @@ public:
inline double cos() const {
#ifdef COUNT_DMS_SINCOS_CALLS
++cachingdms_delta;
m_cacheUsed = true;
#endif
return m_cos; }
......@@ -190,6 +206,7 @@ private:
: dms( degrees ), m_sin( sine ), m_cos( cosine ) {
#ifdef COUNT_DMS_SINCOS_CALLS
++cachingdms_constructor_calls;
m_cacheUsed = false;
#endif
}
......@@ -202,9 +219,12 @@ private:
friend CachingDms operator -(const CachingDms &, const CachingDms &);
#ifdef COUNT_DMS_SINCOS_CALLS
private:
mutable bool m_cacheUsed;
public:
static unsigned long cachingdms_constructor_calls;
static long cachingdms_delta; // difference of ( trig function calls ) - ( trig computations )
static unsigned long cachingdms_bad_uses;
#endif
};
......
......@@ -188,6 +188,7 @@ void DeepStarComponent::draw( SkyPainter *skyp ) {
#ifdef PROFILE_SINCOS
long trig_calls_here = - dms::trig_function_calls;
long trig_redundancy_here = - dms::redundant_trig_function_calls;
long cachingdms_bad_uses = -CachingDms::cachingdms_bad_uses;
dms::seconds_in_trig = 0.;
#endif
SkyMap *map = SkyMap::Instance();
......@@ -321,10 +322,12 @@ void DeepStarComponent::draw( SkyPainter *skyp ) {
#ifdef PROFILE_SINCOS
trig_calls_here += dms::trig_function_calls;
trig_redundancy_here += dms::redundant_trig_function_calls;
cachingdms_bad_uses += CachingDms::cachingdms_bad_uses;
qDebug() << "Spent " << dms::seconds_in_trig << " seconds doing " << trig_calls_here << " trigonometric function calls amounting to an average of " << 1000.0 * dms::seconds_in_trig/double( trig_calls_here ) << " ms per call";
qDebug() << "Redundancy of trig calls in this draw: " << double( trig_redundancy_here ) / double( trig_calls_here ) * 100. << "%";
qDebug() << "CachedDms constructor calls so far: " << CachingDms::cachingdms_constructor_calls;
qDebug() << "Caching has prevented " << CachingDms::cachingdms_delta << " redundant trig function calls";
qDebug() << "Bad cache uses in this draw: " << cachingdms_bad_uses;
#endif
#else
Q_UNUSED(skyp)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment