i writing code utilizes numba jit compile python code. function takes in 2 arrays of same length input, randomly selects slicing point , returns tuple 2 frankenstein array formed parts of 2 input strings. numba not yet support numpy.concatenate function (don't know if ever will). unwilling drop numpy, know performant solution concatenating 2 numpy arrays without concatenate function?
def randomslice(str1, str2): lenstr = len(str1) rnd = np.random.randint(1, lenstr) return (np.concatenate((str1[:rnd], str2[rnd:])), np.concatenate((str2[:rnd], str1[rnd:])))
this might work you:
import numpy np import numba nb @nb.jit(nopython=true) def randomslice_nb(str1, str2): lenstr = len(str1) rnd = np.random.randint(1, lenstr) out1 = np.empty_like(str1) out2 = np.empty_like(str1) out1[:rnd] = str1[:rnd] out1[rnd:] = str2[rnd:] out2[:rnd] = str2[:rnd] out2[rnd:] = str1[rnd:] return (out1, out2)
on machine, using numba 0.27 , timing via timeit
module make sure i'm not counting jit time in stats (or run once, , time subsequent calls), numba version gives small non-negligible performance increase on various size input arrays of ints or floats. if arrays have dtype of |s1
, numba slower. numba team has spent little time optimizing non-numeric usecases isn't terribly surprising. i'm little unclear exact form of input arrays str1
, str2
, can't guarantee code work specific usecase.
Comments
Post a Comment