diff --git a/simgear/timing/sg_time.cxx b/simgear/timing/sg_time.cxx index 9e31ef47..532ec7bf 100644 --- a/simgear/timing/sg_time.cxx +++ b/simgear/timing/sg_time.cxx @@ -32,6 +32,7 @@ #include #include +#include #ifdef HAVE_SYS_TIME_H # include // for get/setitimer, gettimeofday, struct timeval @@ -62,6 +63,9 @@ static const double SIDRATE = 0.9972695677; // tzContainer stores all the current Timezone control points/ std::unique_ptr static_tzContainer; +using StartTimeByYearHash = std::unordered_map; +static StartTimeByYearHash global_yearToGMTHash; + void SGTime::init( const SGGeod& location, const SGPath& root, time_t init_time ) { gst_diff = -9999.0; @@ -143,12 +147,19 @@ static double sidereal_precise( double mjd, double lng ) // return a courser but cheaper estimate of sidereal time static double sidereal_course( time_t cur_time, const struct tm *gmt, double lng ) { - time_t start_gmt, now; + time_t now; double diff, part, days, hours, lstTmp; now = cur_time; - start_gmt = sgTimeGetGMT(gmt->tm_year, 2, 21, 12, 0, 0); - + + // sgTimeGetGMT is expensive when called frequently, so cache the + // result. + time_t start_gmt = global_yearToGMTHash[gmt->tm_year]; + if (start_gmt == 0) { + start_gmt = sgTimeGetGMT(gmt->tm_year, 2, 21, 12, 0, 0); + global_yearToGMTHash[gmt->tm_year] = start_gmt; + } + diff = (now - start_gmt) / (3600.0 * 24.0); part = fmod(diff, 1.0);