 Tags IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 # Can i use Numba to make this piece of python code run fast?

By : Anna Gwynne
Date : September 16 2020, 02:00 AM
this one helps. you could numba-njit a Python implementation of the Haversine distance: code :
``````from math import sin, cos, sqrt, atan2, radians
from numba import njit

@njit
def calc_latlon_dist(lat, lon):
"""
calculate Haversine distance along lat/lon coordinates
"""
R = 6373.0 # approximate radius of earth in km
dist = 0.

for j in range(lat.shape-1):

dlon = lon1 - lon0
dlat = lat1 - lat0

a = sin(dlat / 2)**2 + cos(lat0) * cos(lat1) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))

dist += R * c

return dist
``````

## How does this piece of code function as fast i/o?

By : kamlesh
Date : March 29 2020, 07:55 AM
it should still fix some issue I am unable to comprehend how this function results in faster i/o in c++. please help me out!! , Assuming first line is: int ch = getchar();
code :
``````while((ch<'0'||ch>'9')&&ch!='-'&&ch!=EOF)
ch=getchar();
``````

## Is there any way I can make this small piece of code shorter? (Python)

By : vincent.lee
Date : March 29 2020, 07:55 AM
wish helps you
code :
``````while all(0 in i for i in [first,second,third,fourth]):
...
``````
``````while any(0 in i for i in [first,second,third,fourth]):
...
``````

## Python - Can you help me make a class from a piece of code I'm repeating?

By : user2546985
Date : March 29 2020, 07:55 AM
Any of those help Just look, which values are constant between this lines and wrap them inside a function and define the others to be parameters.
code :
``````def make_label(my_text, my_offset):
temp = tk.Label(frame, text=my_text, anchor='nw')
temp.config(width=30, font=('Microsoft JhengHei Light', 15), bg='#08295e', fg='white')
temp.place(relx=0.05, rely=my_offset)
return temp

instruct1 = make_label('Step1: Do step 1', 0.1)
instruct2 = make_label('Step2: Do step 2', 0.2)
``````

## numpy faster than numba and cython , how to improve numba code

By : Blaise Vignon
Date : March 29 2020, 07:55 AM
should help you out As we will see the behavior is dependent on which numpy-distribution is used.
This answer will focus on Anacoda-distribution with Intel's VML (vector math library), millage can vary given another hardware and numpy-version.
code :
``````N,M=2*10**4, 10**3
a=np.random.rand(N, M)
``````
``````%timeit py_expsum(a)  #   87ms
%timeit nb_expsum(a)  #  672ms
%timeit nb_expsum2(a)  #  412ms
``````
``````Overhead  Command  Shared Object                                      Symbol
62,56%  python   libm-2.23.so                                       [.] __ieee754_exp_avx
16,16%  python   libm-2.23.so                                       [.] __GI___exp
5,25%  python   perf-28936.map                                     [.] 0x00007f1658d53213
2,21%  python   mtrand.cpython-37m-x86_64-linux-gnu.so             [.] rk_random
``````
``````  31,84%  python   libmkl_vml_avx.so                                  [.] mkl_vml_kernel_dExp_E9HAynn                                   ▒
9,47%  python   libiomp5.so                                        [.] _INTERNAL_25_______src_kmp_barrier_cpp_38a91946::__kmp_wait_te▒
6,21%  python   [unknown]                                          [k] 0xffffffff8140290c                                            ▒
5,27%  python   mtrand.cpython-37m-x86_64-linux-gnu.so             [.] rk_random
``````
``````%%cython -L=<path_mkl_libs> --link-args=-Wl,-rpath=<path_mkl_libs> --link-args=-Wl,--no-as-needed -l=mkl_intel_ilp64 -l=mkl_core -l=mkl_gnu_thread -l=iomp5
# path to mkl can be found via np.show_config()

# another option would be to wrap mkl.h:
cdef extern from *:
"""
// MKL_INT is 64bit integer for mkl-ilp64
// see https://software.intel.com/en-us/mkl-developer-reference-c-c-datatypes-specific-to-intel-mkl
#define MKL_INT long long int
void  vdExp(MKL_INT n, const double *x, double *y);
"""
void vdExp(long long int n, const double *x, double *y)

def cy_expsum(const double[:,:] v):
cdef:
double w;
int n = v.size
int current = 0;
double res = 0.0
int size = 0
int i = 0
while current<n:
size = n-current
if size>1024:
size = 1024
vdExp(size, &v[0,0]+current, w)
for i in range(size):
res+=w[i]
current+=size
return res
``````
`````` import numexpr as ne
def ne_expsum(x):
return ne.evaluate("sum(exp(x))")
``````
``````import numpy as np
def py_expsum(x):
return np.sum(np.exp(x))

import numba as nb
@nb.jit( nopython=True)
def nb_expsum(x):
nx, ny = x.shape
val = 0.0
for ix in range(nx):
for iy in range(ny):
val += np.exp( x[ix, iy] )
return val

@nb.jit( nopython=True, parallel=True)
def nb_expsum2(x):
nx, ny = x.shape
val = 0.0
for ix in range(nx):
for iy in nb.prange(ny):
val += np.exp( x[ix, iy]   )
return val

import perfplot
factor = 1.0 # 0.0 or 1e4
perfplot.show(
setup=lambda n: factor*np.random.rand(1,n),
n_range=[2**k for k in range(0,27)],
kernels=[
py_expsum,
nb_expsum,
nb_expsum2,
],
logx=True,
logy=True,
xlabel='len(x)'
)
``````

## How can I make Numba access arrays as fast as Numpy can?

By : Shauno
Date : March 29 2020, 07:55 AM
it should still fix some issue
I'm not expecting Numba to be faster but certainly not 70% slower!
code :
``````%load_ext cython

%%cython
cimport cython

@cython.boundscheck(False)  # Deactivate bounds checking
@cython.wraparound(False)   # Deactivate negative indexing.
cpdef copyto_cython(double[::1] in_array, double[::1] out_array):
cdef Py_ssize_t idx
for idx in range(len(in_array)):
out_array[idx] = in_array[idx]
``````
``````import numpy as np
import numba as nb

def copyto_numpy(a, b):
np.copyto(a, b, 'no')

@nb.jit(nopython=True)
def copyto_numba(a, b):
N = len(a)
for i in range(N):
b[i] = a[i]

``````
``````from simple_benchmark import BenchmarkBuilder, MultiArgument

b = BenchmarkBuilder() 