由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 求改进小函数
相关主题
which str_cmp code is better?请问如何对付error C2148问题:陣列的總大小不能超過 0x7fffffff 位元組
请教如何使用qsort() to sort string.code question
请教一个字符串比较排序的问题问一下C语言编CGI的路径问题
C++ STL map find does not work ???c++ question
C++: use char * in Map请教sgi hash_map
c++ string 一问string /File IO processing using C
strcmp不用include ???如何让这个cur变量正确指向新地址
C库函数的实现帮忙看看这几段程序有问题吗?
相关话题的讨论汇总
话题: str1话题: c2i话题: c1i话题: str2话题: length
进入Programming版参与讨论
1 (共1页)
d****p
发帖数: 685
1
一个比较两个字符串的函数(相等返回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)
y****e
发帖数: 23939
2
直接用STL的equal呢?
inline bool eq_nocase(char c1, char c2) {
return toupper(c1) == toupper(c2);
}
const int N = strlen(str1);
equal(str1, str1+N, str2, eq_nocase);
X****r
发帖数: 3557
3
为什么不用strcasecmp?

【在 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
发帖数: 685
4
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
发帖数: 685
5
Thanks.
I guess this function is more general purposed and should be slower than the
code posted? Anyway I will do a profiling.

【在 y****e 的大作中提到】
: 直接用STL的equal呢?
: inline bool eq_nocase(char c1, char c2) {
: return toupper(c1) == toupper(c2);
: }
: const int N = strlen(str1);
: equal(str1, str1+N, str2, eq_nocase);

g*****g
发帖数: 34805
6
http://www.docjar.com/html/api/java/lang/String.java.html
你可以看看这个源码。

【在 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
发帖数: 3557
7
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.

d****p
发帖数: 685
8
Thanks.

【在 g*****g 的大作中提到】
: http://www.docjar.com/html/api/java/lang/String.java.html
: 你可以看看这个源码。

d****p
发帖数: 685
9
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
发帖数: 2330
10
不能预先都转化为 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.

相关主题
c++ string 一问请问如何对付error C2148问题:陣列的總大小不能超過 0x7fffffff 位元組
strcmp不用include ???code question
C库函数的实现问一下C语言编CGI的路径问题
进入Programming版参与讨论
d****p
发帖数: 685
11
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
发帖数: 946
12
如果是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
发帖数: 10904
13
有些标点符号比 upper case 大,所以不行。

【在 y***d 的大作中提到】
: 不能预先都转化为 upper case 的么?
r****t
发帖数: 10904
14
转成 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
发帖数: 685
15
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

1 (共1页)
进入Programming版参与讨论
相关主题
帮忙看看这几段程序有问题吗?C++: use char * in Map
在用C写shell,遇到问题c++ string 一问
c: compare ampersand & symbol with a stringstrcmp不用include ???
包子问使用C templates Sort data的问题C库函数的实现
which str_cmp code is better?请问如何对付error C2148问题:陣列的總大小不能超過 0x7fffffff 位元組
请教如何使用qsort() to sort string.code question
请教一个字符串比较排序的问题问一下C语言编CGI的路径问题
C++ STL map find does not work ???c++ question
相关话题的讨论汇总
话题: str1话题: c2i话题: c1i话题: str2话题: length