I am unable to speed up computations with my cythonized version of a function bulk_phone_finder defined below (that uses phonenumbers pypi library) to :
from phonenumbers import PhoneNumberMatcher
from phonenumbers.phonenumberutil import SUPPORTED_REGIONS
def bulk_phone_finder(l):
return [get_phonenumbers(content) for content in l]
def get_phonenumbers(content):
return set([m.raw_string for cc in SUPPORTED_REGIONS for m in PhoneNumberMatcher(content,
cc)
])
I am a bit new to cython , but i managed to make a cython equivalent function :
# cython: c_string_type=unicode, c_string_encoding=utf8
from phonenumbers import PhoneNumberMatcher
from phonenumbers.phonenumberutil import SUPPORTED_REGIONS
from libcpp.vector cimport vector
from libcpp.string cimport string
from libcpp.set cimport set as c_set
cpdef c_set[string] _SUPPORTED_REGIONS = SUPPORTED_REGIONS
cdef get_phonenumbers(string content):
res = set()
cdef string cc
cdef string raw_string
for cc in _SUPPORTED_REGIONS :
for m in PhoneNumberMatcher(content, cc):
raw_string = m.raw_string
res.add(raw_string)
return res
def bulk_phone_finder(l):
res=[]
cpdef c_set[string] int_res
cpdef string content
for content in l :
int_res= get_phonenumbers(content)
res.append(int_res)
return res
this cython .pyx file is compiled with the following setup.py
from distutils.core import setup, Extension
from Cython.Build import cythonize
setup( ext_modules = cythonize(Extension(
"bulk_phonenumber_finder",
sources=["bulk_phonenumber_finder.pyx"],
# extra_compile_args=["-std=c++11"],
language="c++"
)))
But i'm unable to get the speed up as :
lines=["0256985412"]*100
# cython version
In [6]: %timeit bulk_phonenumber_finder.bulk_phone_finder(lines)
9.39 s ± 363 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
# python version
In [8]: %timeit bulk_phone_finder(lines)
9.44 s ± 213 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Do you have any advice to speed up my function bulk_phone_finder ?
Any help is more than welcome ,
Thanks in advance,