From c611b50d3f76f9032bcd398bdcb973d7976a4d80 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Thu, 27 Jun 2019 14:22:59 -0400 Subject: [PATCH] Significant performance boost to Python code: eliminate _ter2cel(). There were 3 calls to _ter2cel(), each of which redundantly called _sidereal_time, which results in 3 calculations of _e_tilt(). Reworked so there is only one call to _e_tilt(). Minor changes to support using cProfile, which is how I found this. --- generate/.gitignore | 1 + generate/template/astronomy.py | 11 ++++------- generate/test.py | 21 +++++++++++++-------- source/python/astronomy.py | 11 ++++------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/generate/.gitignore b/generate/.gitignore index c642a6d3..77bf8c3c 100644 --- a/generate/.gitignore +++ b/generate/.gitignore @@ -6,3 +6,4 @@ html/ .ipynb_checkpoints ctest bin/ +profile/ diff --git a/generate/template/astronomy.py b/generate/template/astronomy.py index a25bd43e..eb5d9e26 100644 --- a/generate/template/astronomy.py +++ b/generate/template/astronomy.py @@ -602,10 +602,6 @@ def _spin(angle, pos1): pos1[2] ] -def _ter2cel(time, vec1): - gast = _sidereal_time(time) - return _spin(-15.0 * gast, vec1) - #---------------------------------------------------------------------------- # BEGIN CalcMoon @@ -1186,9 +1182,10 @@ def Horizon(time, observer, ra, dec, refraction): une = [-sinlat*coslon, -sinlat*sinlon, coslat] uwe = [sinlon, -coslon, 0.0] - uz = _ter2cel(time, uze) - un = _ter2cel(time, une) - uw = _ter2cel(time, uwe) + angle = -15.0 * _sidereal_time(time) + uz = _spin(angle, uze) + un = _spin(angle, une) + uw = _spin(angle, uwe) p = [cosdc*cosra, cosdc*sinra, sindc] diff --git a/generate/test.py b/generate/test.py index 9124cbad..9219f198 100644 --- a/generate/test.py +++ b/generate/test.py @@ -46,11 +46,12 @@ def Test_GeoMoon(): print('Test_GeoMoon: EXCESSIVE ERROR') sys.exit(1) -def Test_AstroCheck(): +def Test_AstroCheck(printflag): time = astronomy.Time.Make(1700, 1, 1, 0, 0, 0) stop = astronomy.Time.Make(2200, 1, 1, 0, 0, 0) observer = astronomy.Observer(29, -81, 10) - print('o {:0.6f} {:0.6f} {:0.6f}'.format(observer.latitude, observer.longitude, observer.height)) + if printflag: + print('o {:0.6f} {:0.6f} {:0.6f}'.format(observer.latitude, observer.longitude, observer.height)) dt = 10 + math.pi/100 bodylist = [ astronomy.BODY_SUN, astronomy.BODY_MOON, astronomy.BODY_MERCURY, astronomy.BODY_VENUS, @@ -63,18 +64,22 @@ def Test_AstroCheck(): name = astronomy.BodyName[body] if body != astronomy.BODY_MOON: pos = astronomy.HelioVector(body, time) - print('v {} {:0.16f} {:0.16f} {:0.16f} {:0.16f}'.format(name, pos.t.tt, pos.x, pos.y, pos.z)) + if printflag: + print('v {} {:0.16f} {:0.16f} {:0.16f} {:0.16f}'.format(name, pos.t.tt, pos.x, pos.y, pos.z)) if body != astronomy.BODY_EARTH: j2000 = astronomy.Equator(body, time, observer, False, False) ofdate = astronomy.Equator(body, time, observer, True, True) hor = astronomy.Horizon(time, observer, ofdate.ra, ofdate.dec, astronomy.REFRACTION_NONE) - print('s {} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f}'.format(name, time.tt, time.ut, j2000.ra, j2000.dec, j2000.dist, hor.azimuth, hor.altitude)) + if printflag: + print('s {} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f}'.format(name, time.tt, time.ut, j2000.ra, j2000.dec, j2000.dist, hor.azimuth, hor.altitude)) pos = astronomy.GeoMoon(time) - print('v GM {:0.16f} {:0.16f} {:0.16f} {:0.16f}'.format(pos.t.tt, pos.x, pos.y, pos.z)) + if printflag: + print('v GM {:0.16f} {:0.16f} {:0.16f} {:0.16f}'.format(pos.t.tt, pos.x, pos.y, pos.z)) j2000 = astronomy.Equator(astronomy.BODY_MOON, time, observer, False, False) ofdate = astronomy.Equator(astronomy.BODY_MOON, time, observer, True, True) hor = astronomy.Horizon(time, observer, ofdate.ra, ofdate.dec, astronomy.REFRACTION_NONE) - print('s GM {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f}'.format(time.tt, time.ut, j2000.ra, j2000.dec, j2000.dist, hor.azimuth, hor.altitude)) + if printflag: + print('s GM {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f} {:0.16f}'.format(time.tt, time.ut, j2000.ra, j2000.dec, j2000.dist, hor.azimuth, hor.altitude)) time = time.AddDays(dt) if len(sys.argv) == 2: @@ -86,8 +91,8 @@ if len(sys.argv) == 2: Test_GeoMoon() sys.exit(0) - if sys.argv[1] == 'astro_check': - Test_AstroCheck() + if sys.argv[1] == 'astro_check' or sys.argv[1] == 'astro_profile': + Test_AstroCheck(sys.argv[1] == 'astro_check') sys.exit(0) print('test.py: Invalid command line arguments.') diff --git a/source/python/astronomy.py b/source/python/astronomy.py index 54810607..f9bf497a 100644 --- a/source/python/astronomy.py +++ b/source/python/astronomy.py @@ -693,10 +693,6 @@ def _spin(angle, pos1): pos1[2] ] -def _ter2cel(time, vec1): - gast = _sidereal_time(time) - return _spin(-15.0 * gast, vec1) - #---------------------------------------------------------------------------- # BEGIN CalcMoon @@ -2024,9 +2020,10 @@ def Horizon(time, observer, ra, dec, refraction): une = [-sinlat*coslon, -sinlat*sinlon, coslat] uwe = [sinlon, -coslon, 0.0] - uz = _ter2cel(time, uze) - un = _ter2cel(time, une) - uw = _ter2cel(time, uwe) + angle = -15.0 * _sidereal_time(time) + uz = _spin(angle, uze) + un = _spin(angle, une) + uw = _spin(angle, uwe) p = [cosdc*cosra, cosdc*sinra, sindc]