Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace scalar instances of np.sqrt, np.cos, etc. with math alternatives #1236

Open
janbridley opened this issue Mar 20, 2024 · 1 comment
Open
Labels
good first issue Good for newcomers task A task to be completed.

Comments

@janbridley
Copy link
Contributor

Description

Calls to numpy square root and trig functions that operate on a single value can be replaced with the corresponding math functions.

Motivation and Context

While the performance difference is small in absolute terms, the relative improvement is on the order of 10x. Changing these instances could yield a small performance bumps with only a little effort.

import numpy as np
import math

x = np.random.rand()  

# sqrt:
%timeit -r 100 -n 1000000 np.sqrt(x)  
> 398 ns ± 14.3 ns per loop (mean ± std. dev. of 100 runs, 1,000,000 loops each)

%timeit -r 100 -n 1000000 math.sqrt(x) 
> 24.3 ns ± 2.24 ns per loop (mean ± std. dev. of 100 runs, 1,000,000 loops each) 

# cos:
%timeit -r 100 -n 1000000 np.cos(x)                                                                                                              
> 395 ns ± 10.1 ns per loop (mean ± std. dev. of 100 runs, 1,000,000 loops each)  

%timeit -r 100 -n 1000000 math.cos(x)                                                                                                            
> 31.3 ns ± 2.55 ns per loop (mean ± std. dev. of 100 runs, 1,000,000 loops each)  

# arcsin:
%timeit -r 100 -n 1000000 np.arcsin(x)                                                                                                           
> 413 ns ± 17.5 ns per loop (mean ± std. dev. of 100 runs, 1,000,000 loops each)   

%timeit -r 100 -n 1000000 math.asin(x)                                                                                                          
> 35.8 ns ± 2.85 ns per loop (mean ± std. dev. of 100 runs, 1,000,000 loops each)   
@tommy-waltmann
Copy link
Collaborator

This is a good thought, however freud modules are compiled with cython. Numpy has its own cython modules and a compiled backend, so its possible that these benchmarks will be different if they are done inside a module compiled with cython.

I would be hesistant to make any changes related to this issue before seeing a cython benchmark.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers task A task to be completed.
Projects
None yet
Development

No branches or pull requests

2 participants