socketref,再見!高德
https://github.com/adoggie
C++博客
::
首頁
::
聯系
::
聚合
::
管理
246 Posts :: 4 Stories :: 312 Comments :: 0 Trackbacks
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(54)
給我留言
查看公開留言
查看私人留言
我參與的團隊
隨筆分類
AGG/GEOS/Cairo/SVG(3)
android
Android(2)
Asm-x86(4)
Box2D
C++/Boost/STL/Template(15)
CEGUI(3)
cocos2d
Crack(8)
Django(7)
Docker
Drupal/Zope
easyMQ(1)
EGL
embbed/arm/x86(4)
Encrept & RSA & AES(1)
firefox & mozilla(1)
Flex(6)
Games/Ogre/libClan(1)
geogebra
gevent(7)
Gps管理(7)
hp openview/snmp(2)
Ice/xmlrpc(7)
IM 系統(3)
ios
JQuery
MapGis(25)
Math(4)
Math&Lear algo
Nginx
numpy(1)
OpenGL/DirectX(2)
openmess
Openscales(3)
OpenScales(10)
OpenSource開源工程(37)
perl/python/php/lua/tcl(28)
PHP(1)
Python(1)
qpid(1)
Red5&FMS(1)
Rpc(3)
swMap(5)
TCE(3)
Tk(1)
unix/linux/solaris/sco-unix/novell(11)
WebGis(44)
WebGL(1)
WebService(10)
WebSocket
WINCE(11)
yacc/lex/compiler(2)
zeromq
視頻監控(10)
隨筆檔案
2016年9月 (1)
2016年8月 (1)
2016年3月 (2)
2016年1月 (1)
2015年6月 (1)
2015年5月 (1)
2015年1月 (1)
2014年10月 (1)
2014年5月 (6)
2014年4月 (5)
2014年3月 (1)
2013年12月 (1)
2013年11月 (2)
2013年10月 (2)
2013年9月 (6)
2013年8月 (1)
2013年5月 (1)
2013年4月 (1)
2012年11月 (2)
2012年9月 (1)
2012年7月 (3)
2012年6月 (2)
2012年5月 (1)
2012年4月 (3)
2012年3月 (10)
2012年2月 (3)
2011年12月 (1)
2011年10月 (1)
2011年8月 (5)
2011年7月 (5)
2011年6月 (10)
2011年5月 (5)
2011年1月 (4)
2010年12月 (3)
2010年11月 (9)
2010年10月 (3)
2010年9月 (7)
2010年8月 (1)
2010年7月 (1)
2010年6月 (2)
2010年5月 (3)
2010年4月 (8)
2010年3月 (1)
2010年2月 (4)
2009年11月 (2)
2009年9月 (5)
2009年8月 (10)
2009年6月 (1)
2009年4月 (2)
2009年2月 (4)
2008年8月 (1)
2008年7月 (35)
2008年6月 (5)
2008年5月 (18)
2008年4月 (3)
2007年2月 (3)
2006年9月 (1)
2006年3月 (11)
2006年2月 (1)
2006年1月 (3)
2005年12月 (5)
2005年11月 (3)
文章分類
Django
flex&django
SMS(1)
文章檔案
2010年11月 (2)
2008年5月 (1)
2005年11月 (1)
相冊
ecompass
my son
self_info
高中同學
監控體系
Gis
OpenSource
gnome.org
gnu.org
sf.net
www.w3c.org
www.w3c.org
搜索
最新評論
1.?re: 真是無奈,前后幾年陸續投騰訊阿里若干研發崗位
@Chipset
有一定道理
--放屁阿狗
2.?re: 真是無奈,前后幾年陸續投騰訊阿里若干研發崗位
你把別人都秒了,別人還敢招呼你?唉,明明3你知道他說的不對,你也得點頭說好。低調點,低調點...
--Chipset
3.?re: 利用pyCrypto進行加密Token
111
--jiangwei
4.?re: 今天去the9.com面試,一些考題,一些想法
評論內容較長,點擊標題查看
--一個不知名的路人。
5.?re: 有問題的c++通信代碼的識別
還new,還mutex_unlock,服.
--tqt
閱讀排行榜
1.?易迅商品抓取(17179)
2.?Centos6.x 如何安裝PyBox2D,PyGame,以及升級Python2.7 (VirtualEvn)(16709)
3.?有問題的c++通信代碼的識別(16306)
4.?微信開發環境的配置部署(PHP)(16228)
5.?TCE - 小型的RPC通信引擎庫 介紹(14888)
評論排行榜
1.?今天去the9.com面試,一些考題,一些想法(68)
2.?讀vnc代碼(21)
3.?加快編譯速度(20)
4.?郵件監控(16)
5.?隨便寫個strcmp()函數,看看大家能否有更簡潔的實現(15)
隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
return true if equal
1
bool
strcmp
( char
*
d
,
char
*
s){
2
if
( d
==
s)
return
true
;
3
while
(
*
d
==*
s
&&
*
d
&&
*
s){
4
d
++
;s
++
;
5
}
6
if
(
*
d
==*
s
&&
*
d
==
0
){
7
return
true
;
8
}
9
return
false
;
10
}
posted on 2008-05-05 02:57
放屁阿狗
閱讀(4055)
評論(15)
編輯
收藏
引用
所屬分類:
unix/linux/solaris/sco-unix/novell
Feedback
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 07:28
herculesinchina
char tmp;
char not_equal = 1;
if((d == s) return true;
if((d == NULL) || (s == NULL)) return false;
do{
tmp = !(*s++);
not_equal = !(tmp == *d++);
}while(!(tmp - not_equal));
if(not_equal)
return false
return true
不過 最高效的實現還是嵌入匯編
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 07:30
herculesinchina
正確的程序
char tmp;
char not_equal = 1;
if((d == s) return true;
if((d == NULL) || (s == NULL)) return false;
do{
tmp = (*s++);
not_equal = !(tmp == *d++);
}while(!((!tmp) - not_equal));
if(not_equal)
return false
return true
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 08:32
不懂
樓主要求的是簡潔,呵呵,一般來說都是要求快速,所以樓主這個命題就算是實現了也沒什么用
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現[未登錄]
2008-05-05 08:46
len
crt中strcmp()返回的是整數值,這樣就可以表示小于,等于,大于.
貼段vc中crt實現吧
int __cdecl strcmp (
const char * src,
const char * dst
)
{
int ret = 0 ;
while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 09:51
孤帆1
建議看看ms的源碼.
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 12:00
螞蟻終結者
作為庫函數效率還是比簡潔重要,建議看下VC CRT的匯編代碼。
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 12:20
zhp
我也寫個吧:
bool strcmp( const char* d,const char * s)
{
if( d==s) return true;
while( *d++==*s++ );
return !(*d||*s);
}
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 12:24
zhp
不追求效率還可以再去一行:
bool strcmp( const char* d,const char * s)
{
while( *d++==*s++ );
return !(*d||*s);
}
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 16:19
肥仔
要有大于0,小于0,等于0的返回值啊,怎么才true false呢?
Return Value
The return value for each of these functions indicates the lexicographic relation of string1 to string2.
Value Relationship of string1 to string2
< 0 string1 less than string2
0 string1 identical to string2
> 0 string1 greater than string2
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 19:21
vitacy
assert(d && s);
if(d==s) return true;
if(len(d) != len(s) ) return false;
while(*d && *d++ == *s++ )
return ! *d;
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 21:21
Wang Feng
int
strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
if (*s1 == 0)
return 0;
++s1;
++s2;
}
return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
}
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 22:00
herculesinchina
個人感覺這段VC CRT的代碼效率不如我的高。
下面是對while循環體的編譯反匯編結果。編譯選項為:
gcc -O3
============================================
VC CRT代碼循環體反匯編結果
----------------------------------------------------------
80483b4: 0f b6 13 movzbl (%ebx),%edx
80483b7: 0f b6 01 movzbl (%ecx),%eax
80483ba: 29 c2 sub %eax,%edx
80483bc: 75 21 jne 80483df <strcmp+0x3f>
80483be: 80 39 00 cmpb $0x0,(%ecx)
80483c1: 75 10 jne 80483d3 <strcmp+0x33>
80483c3: eb 1a jmp 80483df <strcmp+0x3f>
80483c5: 0f b6 41 01 movzbl 0x1(%ecx),%eax
80483c9: 83 c3 01 add $0x1,%ebx
80483cc: 83 c1 01 add $0x1,%ecx
80483cf: 84 c0 test %al,%al
80483d1: 74 0c je 80483df <strcmp+0x3f>
80483d3: 0f b6 53 01 movzbl 0x1(%ebx),%edx
80483d7: 0f b6 41 01 movzbl 0x1(%ecx),%eax
80483db: 29 c2 sub %eax,%edx
80483dd: 74 e6 je 80483c5 <strcmp+0x25>
共16條語句
=================================================
我的代碼反匯編結果
--------------------------------------------------------------
80483c0: 0f b6 01 movzbl (%ecx),%eax
80483c3: 83 c1 01 add $0x1,%ecx
80483c6: 3a 03 cmp (%ebx),%al
80483c8: 0f 95 c2 setne %dl
80483cb: 83 c3 01 add $0x1,%ebx
80483ce: 84 c0 test %al,%al
80483d0: 89 d6 mov %edx,%esi
80483d2: 0f 94 c0 sete %al
80483d5: 0f be d2 movsbl %dl,%edx
80483d8: 0f b6 f8 movzbl %al,%edi
80483db: 39 d7 cmp %edx,%edi
80483dd: 74 e1 je 80483c0 <strcmp+0x20>
==================================================
從指令函數上看,循環體少了4條指令,且中間沒有任何跳轉指令,不會影響指令流水線執行。運行效率應該比VC CRT版本高得多。
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-05 22:32
herculesinchina
我的代碼好體現在三點:
1、訪存次數少(一次循環僅訪存兩次)
對于VC代碼,雖然cache一般都會命中,訪問速度肯定比訪問寄存器慢些
2、循環體內無跳轉指令,利于流水線操作
3、指令少。
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-07 10:27
Louix
strcmp表面上看是比較字符串,說白了就是比較兩段內存,為什么只用char *呢?用int *或者__int64 *才會帶來質的飛躍,對于字符串結尾0的處理參考strlen。
回復
更多評論
#
re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
2008-05-08 00:27
放屁阿狗
@Louix
這位老兄說的非常有道理
回復
更多評論
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
郵件監控
好些年前寫的發送mail的shell腳本
btsc 代碼更新 串行數據協議傳輸,增加明文 CRC校驗
操作Solid 數據庫代碼
python連接DB2
cron介紹
【Linux Kernel】 讀碼時寫的一些筆記,畢竟寫給自己看,有些亂(好好理解kernel,能更好寫出高效的驅動和應用代碼)
今天去the9.com面試,一些考題,一些想法
隨便寫個strcmp()函數,看看大家能否有更簡潔的實現
tinyRpc 基于tcp/xml的rpc服務器 [dev...]
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Copyright @ 放屁阿狗
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
国内精品久久久久久久亚洲
|
久久av高潮av无码av喷吹
|
国产精品久久久久久久久久影院
|
91性高湖久久久久
|
亚洲国产成人精品女人久久久
|
精品久久久久久中文字幕
|
久久精品中文字幕有码
|
欧美va久久久噜噜噜久久
|
99热都是精品久久久久久
|
少妇无套内谢久久久久
|
一本久久久久久久
|
欧美亚洲色综久久精品国产
|
99久久国产免费福利
|
亚洲精品tv久久久久久久久
|
青青草国产97免久久费观看
|
久久免费视频观看
|
精品久久人妻av中文字幕
|
亚洲精品无码久久久
|
亚洲国产天堂久久综合网站
|
日韩精品久久久久久免费
|
久久久久亚洲av综合波多野结衣
|
色天使久久综合网天天
|
国产成人无码精品久久久久免费
|
精品无码久久久久久尤物
|
久久午夜无码鲁丝片秋霞
|
久久久WWW成人免费毛片
|
99久久精品国产综合一区
|
日韩欧美亚洲综合久久影院Ds
|
久久精品国产亚洲欧美
|
99久久久国产精品免费无卡顿
|
亚洲乱码中文字幕久久孕妇黑人
|
一本大道久久香蕉成人网
|
亚洲欧洲精品成人久久奇米网
|
日本久久久久久久久久
|
日日狠狠久久偷偷色综合免费
|
国产精品成人精品久久久
|
国产91色综合久久免费分享
|
…久久精品99久久香蕉国产
|
久久香蕉一级毛片
|
久久久久亚洲精品男人的天堂
|
久久www免费人成精品香蕉
|