I want to find elements in an string array. I used two methods, python functions and compiling C++ function and using ctypes to handle in python scripts.
However, I found this way does not speed up the process. For example there is a very large array, and each element has a series of operations, such as replace, delete, and compare, the faster way should compiled the function by C++ code, but it doesn't work, how can I speed up python execution time?
Thanks for your replies.
C++ code:
extern "C" char* func(const char str1[][30], const char* str2, int size) {
//size_t len = strlen(str2);
//char* tmp = new char[len];
string s1, s2;
char* buf = new char[30];
s2 = str2;
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
int n = 0;
for (int i = 0; i < size; i++) {
n += 1;
s1 = str1[i];
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
if (0 == s1.compare(s2)) {
s1 = str1[i];
strcpy(buf, s1.c_str());
break;
}
}
if (n == size) {
s1 = "notfind";
strcpy(buf, s1.c_str());
}
return buf;
}
Python code:
import ctypes as ct
import numpy as np
import datetime
def findvalue(strList,s):
n=0
for i in strList:
n=n+1
i=i.lower()
if s==i:
result=s
break
if n==len(strList):
result='not find'
return result
strList = ['AFG','bB','ccc','AFG','bB','ccc',
'AFG','bB','ccc','AFG','bB','ccc',
'AFG','bB','ccc','AFG','bB','ccc',
'AFG','bB','ccc','AFG','bB','ccc',
'AFG','bB','ccc','AFG','bB','ccc',
'AFG','bB','ccc','AFG','bB','kapa']
s2='kapa'
#####Method1:use ctypes
start=datetime.datetime.now()
dll = ct.CDLL('DLL1.dll')
dll.func.restype = ct.POINTER(ct.c_char_p)
size=len(strList)
strList=np.array(strList)
listtemp=[ct.create_string_buffer(i.encode(), 20) for i in strList]
List = ((ct.c_char * 20) *size)(*listtemp)
cdata2 = ct.c_char_p(s2.encode())
p = dll.func(List, cdata2,size)
s = ct.cast(p, ct.c_char_p)
s = str(s.value, encoding="utf-8")
end=datetime.datetime.now()
sec=(end-start).total_seconds()
print("the time is: " +"{:.20f}".format(sec)+ " s")
#####Method2:use python function
start=datetime.datetime.now()
s2=s2.lower()
findvalue(strList,s2)
end=datetime.datetime.now()
sec=(end-start).total_seconds()
print("the time is: " +"{:.20f}".format(sec)+ " s")