比STL字符串类更快? [英] Faster than STL string class?

查看:57
本文介绍了比STL字符串类更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我最近学习了C ++并且我开了一个字符串类。

代码示例如下:


class CString

{

public:

inline CString(const char * rhs)

{

m_size = strlen(rhs);

m_capacity = m_size * 2 + 1;

m_str = new char [m_capacity];

memmove(m_str,rhs,m_size + 1);

}


inline CString& __cdecl operator + =(const CString& rhs)

{

unsigned __int32 tmp_size = m_size + rhs.m_size;


if( m_capacity< = tmp_size)

{

m_capacity = tmp_size * 2 + 1;

char * tmp_char = new char [m_capacity];

memmove(tmp_char,m_str,m_size);

删除m_str;

m_str = tmp_char;

}


memmove(m_str + m_size,rhs.m_str,rhs.m_size);

m_size = tmp_size;

return * this; < br $>
}

私人:

char * m_str;

unsigned __int32 m_capacity;

unsigned __int32 m_size;

};

// stl string class

string tmp1 =" 123123";

string tmp2 =" 999999" ;;

for(int i = 0; i< 50000; ++ i)

tmp1 + = tmp2;

//1936033.000000微秒


//CString class

CString tmp1 =" 123123";

CString tmp2 =" 999999";

for(int i = 0; i< 50000; ++ i)

tmp1 + = tmp2;

// @ 21106.333333微秒


有任何错误或有害代码CString class?

为什么选择CString?比STL字符串类快吗?

许多书都说你不应该开发一个已开发的类

在这种情况下,哪个更好?

谁可以帮我做出选择?


问候,

YinTat

Hi,
I learned C++ recently and I made a string class.
A code example is this:

class CString
{
public:
inline CString(const char *rhs)
{
m_size = strlen(rhs);
m_capacity = m_size * 2 + 1;
m_str = new char[m_capacity];
memmove(m_str, rhs, m_size+1);
}

inline CString& __cdecl operator+= (const CString& rhs)
{
unsigned __int32 tmp_size = m_size + rhs.m_size;

if (m_capacity <= tmp_size)
{
m_capacity = tmp_size * 2 + 1;
char *tmp_char = new char[m_capacity];
memmove(tmp_char, m_str, m_size);
delete m_str;
m_str = tmp_char;
}

memmove(m_str+m_size, rhs.m_str, rhs.m_size);
m_size = tmp_size;
return *this;
}
private:
char *m_str;
unsigned __int32 m_capacity;
unsigned __int32 m_size;
};
//stl string class
string tmp1 = "123123";
string tmp2 = "999999";
for(int i=0;i<50000;++i)
tmp1 += tmp2;
//1936033.000000 microsecond

//the "CString" class
CString tmp1 = "123123";
CString tmp2 = "999999";
for(int i=0;i<50000;++i)
tmp1 += tmp2;
//21106.333333 microsecond

Have any bug or harmful code in the "CString" class?
Why the "CString" class faster than STL string class?
Many books said that "You should not develop a class that have been developed"
In this case, Which is better?
Who can help me make a choice?

Regards,
YinTat

推荐答案

YinTat写道:

我的结果不同:
YinTat wrote:
My results are different:
// stl string class
string tmp1 =" 123123" ;;
string tmp2 =" 999999" ;;
for(int i = 0; i< 50000; ++ i)
******** tmp1 + = tmp2 ;
//1936033.000000微秒


9780微秒//CString class
CString tmp1 =" 123123";
CString tmp2 =" 999999" ;;
for(int i = 0; i< 50000; ++ i)
tmp1 + = tmp2;
// 21106.333333微秒


4147微秒


仍然,你的速度比标准字符串要快得多,但是

问题是你的基准有多接近类似于

字符串的典型用法。

在CString中有任何错误或有害代码。 class?
//stl string class
string tmp1 = "123123";
string tmp2 = "999999";
for(int i=0;i<50000;++i)
********tmp1 += tmp2;
//1936033.000000 microsecond
9780 microseconds //the "CString" class
CString tmp1 = "123123";
CString tmp2 = "999999";
for(int i=0;i<50000;++i)
tmp1 += tmp2;
//21106.333333 microsecond
4147 microseconds

I wonder why string handling is so horribly slow on your system, or was
that on some embedded system or very old PC?
Still, yours is quite a bit faster than the standard string, but the
question is how closely your "benchmark" resembles the typical use of
strings.
Have any bug or harmful code in the "CString" class?




它不是线程安全的。



It is not thread safe.


YinTat写道:

我最近学习了C ++并且创建了一个字符串类。
一个代码示例是这样的:

类CString
{
[snip]};
[snip]


顺便说一句,Microsoft编译器已经有一个名为

CString的类。


名称是否应更改为CppString?

AC string通常是指一个空终止数组

个字符;这是C语言中的字符串。


请参阅:
http://www.jelovic.com/articles/stupid_naming.htm

问候,
YinTat
Hi,
I learned C++ recently and I made a string class.
A code example is this:

class CString
{ [snip] }; [snip]

By the way, the Microsoft Compiler already has a class called
CString.

Should the name be changed to CppString?
A "C" string generally refers to a null terminated array
of characters; which is a string in the C language sense.

See:
http://www.jelovic.com/articles/stupid_naming.htm

Regards,
YinTat



-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc -c ++ / faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Rolf Magnus写道:
Rolf Magnus wrote:
YinTat写道:

我的结果不同:
YinTat wrote:
My results are different:
// stl string class
string tmp1 =" 123123" ;;
string tmp2 =" 999999" ;;
for(int i = 0; i< 50000; ++ i )
tmp1 + = tmp2;
//1936033.000000微秒
//stl string class
string tmp1 = "123123";
string tmp2 = "999999";
for(int i=0;i<50000;++i)
tmp1 += tmp2;
//1936033.000000 microsecond



9780微秒



9780 microseconds

//CString class
CString tmp1 =" 123123";
CString tmp2 =" 999999" ;;
for(int i = 0; i< 50000; ++ i)
tmp1 + = tmp2;
//21106.333333微秒
//the "CString" class
CString tmp1 = "123123";
CString tmp2 = "999999";
for(int i=0;i<50000;++i)
tmp1 += tmp2;
//21106.333333 microsecond



4147微秒

我想知道为什么字符串处理在你的系统上非常慢,或者是仍然,你的标准字符串要快得多,但问题是你的基准是多么紧密。类似于
字符串的典型用法。



4147 microseconds

I wonder why string handling is so horribly slow on your system, or was
that on some embedded system or very old PC?
Still, yours is quite a bit faster than the standard string, but the
question is how closely your "benchmark" resembles the typical use of
strings.

在CString中有任何错误或有害代码。 class?
Have any bug or harmful code in the "CString" class?



它不是线程安全的。



It is not thread safe.



看起来OP的std :: string类遭受O( n ^ 2)

损坏,例如,一些旧版本的MFC CString

实现。不过已经修复了这个问题。


在我的机器上,手写的CString类击败了std :: string但不是因为
因子为2.但是,它应该指出,在速度 - 空间权衡中有一个明显的

参数:CString将分配的

内存加倍。如果该因素减少,它肯定会浪费更少的内存,而代价是更多的分配

。类似地,人们仍然可以通过增加该因子来加速它。


此外,在许多实现中,std :: string试图明白

作业。在基于引用计数的实现中,很多时候字符串的分配只会导致指针副本多一点。

因此,给定项目的哪个实现更好是很难

预测。在任何情况下,我都怀疑增益是否值得实际编写完整的字符串类。

。好吧,除非当然std :: string

完全很糟糕,就像OP的情况一样。

最好


Kai-Uwe


It appears as if the std::string class from the OP suffers from a O(n^2)
flaw that was hurting, e.g., some older version of MFC CString
implementations. This has been fixed, though.

On my machine the handcoded CString class beats std::string but not by a
factor of 2. However, it should be pointed out, that there is an obvious
parameter in the speed-space tradeoff: CString doubles the allocated
memory. It would surely waste less memory at the cost of more allocations
if that factor was decreased. Similarly, one could still speed it up by
increasing that factor.

Moreover, on many implementations, std::string is trying to be smart about
assignments. In reference count based implementations, many times an
assignment of strings will result in only little more than a pointer copy.
Thus, which implementation is better for a given project is very hard to
predict. In any case, I would doubt that the gain is worth the effort of
actually coding a complete string class. Well, unless of course std::string
completely sucks as appears to be the case with the OP.
Best

Kai-Uwe


这篇关于比STL字符串类更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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