특정 길이의 가능한 모든 하위 문자열에 대해 일부 처리를 수행하는 프로그램이 있습니다. 가능한 한 빨리 프로그램을 만들려고 노력하고 있습니다. 나는 그것을 더 빨리 만들기 위해 다음 프로그램을 어떻게 할 수 있을지 궁금해하고있다.더 빠른 부분 문자열 처리 C++
char str[] = "abcdcddcdcdcdcd....................." // large string
int n = strlen(str), m = 20;
for(int i=0; i<n; i++){
char *substr = (char*) malloc(sizeof(char)*m);
strncpy(substr, str+i, m);
// do some processing
int h = hd(substr, X) // X is another string of same length
free(substr);
}
unsigned int hd(const std::string& s1, const std::string& s2)
{
return std::inner_product(
s1.begin(), s1.end(), s2.begin(),
0, std::plus<unsigned int>(),
std::not2(std::equal_to<std::string::value_type>())
);
}
이것은 C++ 코드가 아닙니다. 실제로 평이한 c와 좀 더 비슷해 보입니다. –
'malloc' 호출을 피하고 루프 외부에서 버퍼를 생성 할 수 있습니다. – Jarod42
예,'string' 대신'char *'를 사용하고 있습니다. string'의'substr()'함수가 객체를 생성한다고 생각합니다. 따라서 큰 문자열에 대해서는 속도가 느려집니다. –