strlen()速度测试 [英] strlen() speed test

查看:136
本文介绍了strlen()速度测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一些我自己的字符串操作例程(例如

strcpy()替代方法,它返回复制的字符数)。我已经用一个更简单的函数strlen()开始了
。我写了一个

非常简单的版本调用StringLength(),但它的表现比它的CRT同行要慢得多。


这里是我的实现:

inline unsigned int StringLength(const char * pszString)

{

unsigned int cch = 0;

while(* pszString ++)

cch ++;

返回cch;

}


我已经使用我自己的计时器将我的版本与strlen()和_mbslen()进行了比较

函数,这是一个围绕Windows的包装器

QueryPerformanceCounter ()API。


我的唯一猜测是CRT版本使用处理器寄存器来计算其计数器和我的版本使用RAM。 (你不能在VC ++编译器中使用

" register"关键字。)


如何使我的字符串操作函数竞争CRT函数的速度是否为


I need to write a couple of my own string manipulation routines (e.g.
a strcpy() alternative that returns the number of chars copied). I''ve
started with one of the simpler functions, strlen(). I''ve written a
very simple version call StringLength(), but it performs significantly
slower than its CRT counterparts.

Here''s my implementation:
inline unsigned int StringLength( const char *pszString )
{
unsigned int cch = 0;
while ( *pszString++ )
cch++;
return cch;
}

I''ve compared my version to strlen() and _mbslen() using my own timer
function, which is a wrapper around the Windows
QueryPerformanceCounter() API.

My only guess is that the CRT versions use the processor registers for
its counters and my version uses RAM. (You can''t make use of the
"register" keyword with the VC++ compiler.)

How can I make my string manipulation functions compete speedwise with
the CRT functions?

推荐答案

Nollie写道:
Nollie wrote:
我需要编写一些我自己的字符串操作例程(例如
一个strcpy()替代方法,它返回复制的字符数)。我已经开始使用一个更简单的函数strlen()。我写了一个非常简单的版本调用StringLength(),但它的表现比CRT同行慢得多。

这是我的实现:
inline unsigned int StringLength(const char * pszString)
{
unsigned int cch = 0;
while(* pszString ++)
cch ++;
return cch;
}


怎么样


inline unsigned int StringLength(const char * pszString)

{

const char * p = pszString;

while(* p)p ++;

return p - pszString;

}

我已经使用自己的计时器
函数将我的版本与strlen()和_mbslen()进行了比较,这是Windows的一个包装器。 /> QueryPerformanceCounter()API。

我唯一的猜测是CRT版本使用处理器寄存器来计算其计数器,我的版本使用RAM。


编译器将决定使用什么。你有没有设置最大优化

级别?

(你不能在VC ++编译器中使用register关键字。)


为什么?

如何让我的字符串操作函数与CRT函数快速竞争?
I need to write a couple of my own string manipulation routines (e.g.
a strcpy() alternative that returns the number of chars copied). I''ve
started with one of the simpler functions, strlen(). I''ve written a
very simple version call StringLength(), but it performs significantly
slower than its CRT counterparts.

Here''s my implementation:
inline unsigned int StringLength( const char *pszString )
{
unsigned int cch = 0;
while ( *pszString++ )
cch++;
return cch;
}
How about:

inline unsigned int StringLength( const char *pszString )
{
const char* p = pszString;
while (*p) p++;
return p - pszString;
}
I''ve compared my version to strlen() and _mbslen() using my own timer
function, which is a wrapper around the Windows
QueryPerformanceCounter() API.

My only guess is that the CRT versions use the processor registers for
its counters and my version uses RAM.
The compiler will decide what to use. Did you set maximum optimization
level?
(You can''t make use of the "register" keyword with the VC++ compiler.)
Why?
How can I make my string manipulation functions compete speedwise with
the CRT functions?




可能有一些优化你可以做(​​例如,不要在循环中查找每个单个字符,而是在

一次检查多个字符,分别处理字符串结尾)。

OTOH,库例程可能会使用一些特殊的汇编指令

来加快速度。像x86这样的系统有特殊的内置字符串

指令,可以快速搜索数组中的特定值。



There might be some optimizations that you can do (e.g. not go though each
single character in the loop but instead checking multiple characters at
once, handling the string end separately).
OTOH, the library routine possibly uses some special assembler instructions
to speed this up. Systems like x86 have special built-in string
instructions to search quickly for a specific value in an array.


> > (你不能在VC ++编译器中使用register关键字。)
>> (You can''t make use of the "register" keyword with the VC++ compiler.)
为什么?
Dunno。这里是MS文档:
http://msdn.microsoft.com/library/de...er_keyword.asp

怎么样
inline unsigned int StringLength(const char * pszString)
{char /> const char * p = pszString;
while(* p)p ++;
return p - pszString ;
}
谢谢!是的,这是一个更好的算法。但是,虽然它确实使得StringLength()更快,但它仍然无法与CRT函数相媲美。

编译器将决定使用什么。你设置了最高优化级别吗?
Why? Dunno. Here''s MS docs:
http://msdn.microsoft.com/library/de...er_keyword.asp
How about:
inline unsigned int StringLength( const char *pszString )
{
const char* p = pszString;
while (*p) p++;
return p - pszString;
} Thank you! Yes, this is a better algorithm. However, though it does
make StringLength() faster, it still does not rival the CRT functions.
The compiler will decide what to use. Did you set maximum optimization
level?



是的,这就是答案。对不起,我应该在

发布之前试过这个。我的计时器只能在未经优化的DEBUG版本中工作,但是我已经将它重写为所有构建版本的b $ b。 StringLength()和strlen()现在

几乎使用完全相同的处理时间。


然而,现在我发现了另一个特点...


比较6个strlen()替代方案:

1. strlen()// CRT函数

2. _tcslen()//冗余,映射到strlen

3. lstrlen()// Windows API

4. StringLength()//我的函数

5. _mbslen() //对于多字节字符串

6. _mbstrlen()//用于多字节字符串


除了_mbslen()之外,每个人几乎使用完全相同的处理时间br />
和_mbstrlen(),几乎是其他人的两倍。我已经安排了测试,以便_mbs *函数首先进行测试,最后一次,

,介于两者之间,所以缓存不是问题。


在我看来_mbs *应该更慢,因为他们必须检查

的双字节字符,但它们确实相当快。为什么这可能是




Yes, this is the answer. Sorry, I should have tried this before
posting. My timer only worked in unoptimized, DEBUG builds, but I''ve
rewritten it to work for all builds. StringLength() and strlen() now
use practically the exact same processing time.

However, now I have discovered another peculiarity...

Comparing 6 strlen() alternatives:
1. strlen() // CRT function
2. _tcslen() // redundant, maps to strlen
3. lstrlen() // Windows API
4. StringLength() // my function
5. _mbslen() // for multibyte strings
6. _mbstrlen() // for multibyte strings

Every one uses almost the exact same processing time, except _mbslen()
and _mbstrlen(), which are almost twice as fast as the others. I''ve
arranged the test so that the _mbs* functions are tested first, last,
and in between, so caching is not an issue.

It seems to me the _mbs* should be slower because they have to check
for double-byte character, but they are indeed quite faster. Why might
this be?




Nollie写道:

Nollie wrote:
(你不能在VC ++编译器中使用
" register"关键字。)
(You can''t make use of the
"register" keyword with the VC++ compiler.)



说谁?


Brian


Says who?

Brian


这篇关于strlen()速度测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆