{"action":"created","issue":{"url":"https://api.github.com/repos/astropy/astropy/issues/3764","labels_url":"https://api.github.com/repos/astropy/astropy/issues/3764/labels{/name}","comments_url":"https://api.github.com/repos/astropy/astropy/issues/3764/comments","events_url":"https://api.github.com/repos/astropy/astropy/issues/3764/events","html_url":"https://github.com/astropy/astropy/issues/3764","id":74480947,"number":3764,"title":"slow comoving_distance in astropy.cosmology","user":{"login":"monodera","id":90394,"avatar_url":"https://avatars.githubusercontent.com/u/90394?v=3","gravatar_id":"","url":"https://api.github.com/users/monodera","html_url":"https://github.com/monodera","followers_url":"https://api.github.com/users/monodera/followers","following_url":"https://api.github.com/users/monodera/following{/other_user}","gists_url":"https://api.github.com/users/monodera/gists{/gist_id}","starred_url":"https://api.github.com/users/monodera/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/monodera/subscriptions","organizations_url":"https://api.github.com/users/monodera/orgs","repos_url":"https://api.github.com/users/monodera/repos","events_url":"https://api.github.com/users/monodera/events{/privacy}","received_events_url":"https://api.github.com/users/monodera/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-05-08T20:16:24Z","updated_at":"2015-05-09T00:34:54Z","closed_at":null,"body":"My colleague pointed out that the `comoving_distance` function in `astropy.cosmology` is a lot slower than equivalent function `comdist2` in `idlutils` by SDSS (https://www.sdss3.org/dr8/software/idlutils.php).  For example, the following IDL code reports 0.04 second for 10^5 elements (and 0.35 sec in the case for a 10^7 elements). \r\n\r\n```idl\r\nz = randomu(1, 100000L)*6\r\nt_begin = systime(/second)\r\nd_l = comdis2(z, 0.3, 0.7, cdtable=cdtable)\r\nt_end = systime(/second)\r\nprint, \"Time elapsed:\", t_end-t_begin\r\nend\r\n```\r\n\r\nOn the other hand, an equivalent python code with `astropy.cosmology` like this takes 23.3 seconds.\r\n\r\n```python\r\nfrom astropy.cosmology import FlatLambdaCDM\r\nimport numpy as np\r\nimport time\r\nz = np.random.rand(100000)*6.\r\nt_begin = time.time()\r\ncosmo_astropy = FlatLambdaCDM(H0=70., Om0=0.3)\r\nd_astropy = cosmo_astropy.comoving_distance(z)\r\nt_end = time.time()\r\nprint(\"Time elapsed with astropy: %f seconds\" % (t_end-t_begin))\r\n```\r\n\r\nInstead of `astropy`, I tried `cosmolopy` (http://roban.github.io/CosmoloPy/) as follows and it takes 5.1 seconds.\r\n\r\n```python\r\nimport numpy as np\r\nimport time\r\nimport cosmolopy.distance as cd\r\ncosmo = {'omega_M_0' : 0.3, 'omega_lambda_0' : 0.7, 'h' : 0.72}\r\ncosmo = cd.set_omega_k_0(cosmo)\r\nz = np.random.rand(100000)*6.\r\nt_begin = time.time()\r\nd_cosmolopy = cd.comoving_distance(z, **cosmo)\r\nt_end = time.time()\r\nprint(\"Time elapsed with cosmolopy: %f seconds\" % (t_end-t_begin))\r\n```\r\nI guess it's not expected and there could be a room to be improved.\r\n\r\nMy computing environment is:\r\nOS X 10.10.3\r\nIDL 8.4\r\nPython 2.7.9 (installed via homebrew)\r\nAstropy 1.0.2\r\nNumpy 1.9.2\r\nCosmolopy 0.1.104 (laterst version installed by pip)"},"comment":{"url":"https://api.github.com/repos/astropy/astropy/issues/comments/100399691","html_url":"https://github.com/astropy/astropy/issues/3764#issuecomment-100399691","issue_url":"https://api.github.com/repos/astropy/astropy/issues/3764","id":100399691,"user":{"login":"aconley","id":2012806,"avatar_url":"https://avatars.githubusercontent.com/u/2012806?v=3","gravatar_id":"","url":"https://api.github.com/users/aconley","html_url":"https://github.com/aconley","followers_url":"https://api.github.com/users/aconley/followers","following_url":"https://api.github.com/users/aconley/following{/other_user}","gists_url":"https://api.github.com/users/aconley/gists{/gist_id}","starred_url":"https://api.github.com/users/aconley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aconley/subscriptions","organizations_url":"https://api.github.com/users/aconley/orgs","repos_url":"https://api.github.com/users/aconley/repos","events_url":"https://api.github.com/users/aconley/events{/privacy}","received_events_url":"https://api.github.com/users/aconley/received_events","type":"User","site_admin":false},"created_at":"2015-05-09T00:34:54Z","updated_at":"2015-05-09T00:34:54Z","body":"Ok -- here's one thing worth trying: if you look at `FLRW.comoving_distance` the call to `inv_efunc` is inside the loop, so Python is looking it up on each call.  That isn't free, so we should assign it to a local variable that we pass into quad.  Again, it's hard to see that giving a factor of 4...\r\n\r\nSome of the expense is also coming from wrapping it in a quantity."}}