一个比较两个字符串的函数(相等返回0,否则-1或者1): int compareStr(str1, str2) if (length(str1) < length(str2)) return -1; else if (length(str1) > length(str2)) return 1; // two string equal in length for each character c1i in str1 and c2i in str2 if c1i is upper case convert c1i to lower case if c2i is upper case convert c2i to lower case if (c1i < c2i)
【在 d****p 的大作中提到】 : 一个比较两个字符串的函数(相等返回0,否则-1或者1): : int compareStr(str1, str2) : if (length(str1) < length(str2)) : return -1; : else if (length(str1) > length(str2)) : return 1; : // two string equal in length : for each character c1i in str1 and c2i in str2 : if c1i is upper case : convert c1i to lower case
d*p
6 楼
The string comes as wstring, not string. Do you see better algorithm? The function is called so many times since it is used to compare keys in a lot of maps.
【在 X****r 的大作中提到】 : 为什么不用strcasecmp?
d*p
7 楼
Thanks. I guess this function is more general purposed and should be slower than the code posted? Anyway I will do a profiling.
【在 d****p 的大作中提到】 : 一个比较两个字符串的函数(相等返回0,否则-1或者1): : int compareStr(str1, str2) : if (length(str1) < length(str2)) : return -1; : else if (length(str1) > length(str2)) : return 1; : // two string equal in length : for each character c1i in str1 and c2i in str2 : if c1i is upper case : convert c1i to lower case
X*r
9 楼
wcscasecmp() if available You can check the source code of strcasecmp to get the idea. However, it really depends on the representation of the string, e.g. checking the length first is not efficient for C strings, but it is for C++'s std::string.
it
【在 d****p 的大作中提到】 : The string comes as wstring, not string. : Do you see better algorithm? The function is called so many times since it : is used to compare keys in a lot of maps.
Will try wcscasecmp - not sure it is available on all platforms. If it doesn't work, I may consider building a lookup table to do the case translation directly.
【在 X****r 的大作中提到】 : wcscasecmp() if available : You can check the source code of strcasecmp to get the idea. However, it : really depends on the representation of the string, e.g. checking the : length first is not efficient for C strings, but it is for C++'s : std::string. : : it
y*d
12 楼
不能预先都转化为 upper case 的么?
【在 d****p 的大作中提到】 : The string comes as wstring, not string. : Do you see better algorithm? The function is called so many times since it : is used to compare keys in a lot of maps.
d*p
13 楼
That's a good idea. However for now it is not realistic since project is close to ending and such a change may be risky - too much code relying on this little function. I would like to say if we were starting from scratch again, this kind of preprocessing is absolutely necessary. I tried using a char map to convert upper char to lower case char. Scaled cpu time spent on the function dropped from 22 to 16, around 27% boost. ie if (c >= L'A' && c < L'Z') c -= L'A' - L'a' to c = charmap[c] where ch
【在 y***d 的大作中提到】 : 不能预先都转化为 upper case 的么?
v*s
14 楼
如果是C /C++, 前面2个strlen应该去掉,合并到后面的循环中。 具体细节自己考虑吧,或者google c strcmp 函数。
【在 d****p 的大作中提到】 : 一个比较两个字符串的函数(相等返回0,否则-1或者1): : int compareStr(str1, str2) : if (length(str1) < length(str2)) : return -1; : else if (length(str1) > length(str2)) : return 1; : // two string equal in length : for each character c1i in str1 and c2i in str2 : if c1i is upper case : convert c1i to lower case
r*t
15 楼
有些标点符号比 upper case 大,所以不行。
【在 y***d 的大作中提到】 : 不能预先都转化为 upper case 的么?
r*t
16 楼
转成 lower case 以后,wstring 有 operator- 没有?如果没有的话做成 union {wstring, long long...},或者 cast 成足够大的整数,然后用整数相减行不行呢?或者 derive wstring 实现减法也行。
is on scratch Scaled boost. ie under
【在 d****p 的大作中提到】 : That's a good idea. However for now it is not realistic since project is : close to ending and such a change may be risky - too much code relying on : this little function. I would like to say if we were starting from scratch : again, this kind of preprocessing is absolutely necessary. : I tried using a char map to convert upper char to lower case char. Scaled : cpu time spent on the function dropped from 22 to 16, around 27% boost. ie : if (c >= L'A' && c < L'Z') c -= L'A' - L'a' : to : c = charmap[c] where ch
d*p
17 楼
casting wstring to large integer is a very interesting concept.thanks.
?或者
【在 r****t 的大作中提到】 : 转成 lower case 以后,wstring 有 operator- 没有?如果没有的话做成 union : {wstring, long long...},或者 cast 成足够大的整数,然后用整数相减行不行呢?或者 : derive wstring 实现减法也行。 : : is : on : scratch : Scaled : boost. : ie