C ++字符串& C字符串 [英] C++ strings & C strings

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

问题描述

我正在做练习4.3.1 - 4.29的C ++ Primer 4 / e。作者,

与运行时显示,声称C ++库字符串比

C风格字符串更快。我在C&因此

发现作者的声明是*部分*。如果我们在C ++而不是Library String类中使用C风格的字符串

,那么它们很慢但是如果在C中写入相同的程序,那么C字符串是更快的;比起C ++

图书馆字符串& C ++中的C风格字符串。以下是3

计划的代码。我将非常感谢您对C ++编程的任何评论,

关于练习,我的代码等。


感谢


// C ++中的C风格字符串


#include< iostream>

#include< cstring>


int main(){

// C样式字符串实现

const char * pc ="一个非常长的文字字符串" ;;

const size_t len = strlen(pc + 1); //对于NULL终止符


//字符串分配和复制的性能测试

for(size_t ix = 0; ix!= 1000000; ++ ix)

{

char * pc2 = new char [len + 1]; //动态空间分配

NULL终止符

strcpy(pc2,pc); //将字符串复制到已分配

空格

if(strcmp(pc2,pc))

;

删除[] pc2;

}

}

----------------- -------------------------------------------------- ------


// C ++库字符串

#include< iostream>

#include< string> ;


int main(){

// C ++标准库字符串实现

std :: string str(" a非常长的文字字符串);


//关于凝视分配的性能tst&复制

//字符串库类自动内存管理

for(unsigned long ix = 0; ix!= 10000000; ++ ix)

{

std :: string str2 = str;

if(str == str2)

;

}


}

---------------------------- ------------------------------


// C string


#include< string.h>


int main(){

char * ps =" a very long文字字符串" ;;

int len = strlen(ps + 1);

unsigned long i;

char * ps2;


for(i = 0; i< = 10000000; ++ i)

{

* ps2 = malloc(len + 1) ;

strcpy(ps2,ps);

if(strcmp(ps2,ps))

;

free(ps2);

ps2 = NULL;

}

}

- arnuld
http://arnuld.blogspot.com

解决方案

* arnuld:


我正在做练习4.3.1 - 4.29的C ++ Primer 4 / e作者,

与运行时显示,声称C ++库字符串比

C风格字符串更快。我在C&因此

发现作者的声明是*部分*。如果我们在C ++而不是Library String类中使用C风格的字符串

,那么它们很慢但是如果在C中写入相同的程序,那么C字符串是更快的;比起C ++

图书馆字符串& C ++中的C风格字符串。以下是3

计划的代码。我将非常感谢您对C ++编程的任何评论,

关于练习,我的代码等。


感谢


// C ++中的C风格字符串


#include< iostream>

#include< cstring>


int main(){

// C样式字符串实现

const char * pc ="一个非常长的文字字符串" ;;

const size_t len = strlen(pc + 1); //对于NULL终止符



发现错误。


//对性能测试字符串分配和复制

for(size_t ix = 0; ix!= 1000000; ++ ix)

{

char * pc2 = new char [len + 1]; //动态空间分配

NULL终止符

strcpy(pc2,pc); //将字符串复制到已分配

空格

if(strcmp(pc2,pc))

;

删除[] pc2;

}

}

----------------- -------------------------------------------------- ------


// C ++库字符串

#include< iostream>

#include< string> ;


int main(){

// C ++标准库字符串实现

std :: string str(" a非常长的文字字符串);


//关于凝视分配的性能tst&复制

//字符串库类自动内存管理

for(unsigned long ix = 0; ix!= 10000000; ++ ix)

{

std :: string str2 = str;

if(str == str2)

;

}


}

---------------------------- ------------------------------


// C string


#include< string.h>


int main(){

char * ps =" a very long文字字符串" ;;

int len = strlen(ps + 1);



发现错误。


unsigned long i;

char * ps2;


for(i = 0; i< = 10000000; ++ i)



发现差异。


{

* ps2 = malloc(len + 1);

strcpy(ps2 ,ps);

if(strcmp(ps2,ps))

;

免费(ps2);

ps2 = NULL;

}

}



-

A :因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的是什么?


arnuld写道:


i正在进行练习4.3.1 - C ++ Primer 4 / e的4.29。作者,

与运行时显示,声称C ++库字符串比

C风格字符串更快。我在C&因此

发现作者的声明是*部分*。如果我们在C ++而不是Library String类中使用C风格的字符串

,那么它们很慢但是如果在C中写入相同的程序,那么C字符串是更快的;比起C ++

图书馆字符串& C ++中的C风格字符串。以下是3

计划的代码。我将非常感谢您对C ++编程的任何评论,

关于练习,我的代码等。


感谢


// C ++中的C风格字符串


#include< iostream>

#include< cstring>


int main(){

// C样式字符串实现

const char * pc ="一个非常长的文字字符串" ;;

const size_t len = strlen(pc + 1); //对于NULL终止符


//字符串分配和复制的性能测试

for(size_t ix = 0; ix!= 1000000; ++ ix)



这个神奇的数字比其他数字短一个数字:


for(size_t ix = 0; ix!= 10000000 ; ++ ix)


{

char * pc2 = new char [len + 1]; //动态空间分配

NULL终止符

strcpy(pc2,pc); //将字符串复制到已分配

空格

if(strcmp(pc2,pc))

;

删除[] pc2;

}

}

----------------- -------------------------------------------------- ------


// C ++库字符串

#include< iostream>

#include< string> ;


int main(){

// C ++标准库字符串实现

std :: string str(" a非常长的文字字符串);


//关于凝视分配的性能tst&复制

//字符串库类自动内存管理

for(unsigned long ix = 0; ix!= 10000000; ++ ix)

{

std :: string str2 = str;

if(str == str2)

;

}


}

---------------------------- ------------------------------


// C string


#include< string.h>



#include< stdlib.h>


>

int main(){

char * ps ="一个非常长的文字字符串" ;;

int len = strlen(ps + 1);

unsigned long i;

char * ps2;


for(i = 0; i< = 10000000; ++ i)

{

* ps2 = malloc(len + 1);



应该是


ps2 = malloc(len + 1);





ps2 =(char *)malloc(len + 1);


strcpy(ps2,ps) ;

if(strcmp(ps2,ps))

;

free(ps2);

ps2 = NULL;

}

}


- arnuld
http://arnuld.blogspot.com



使用固定程序,我得到:


第一个节目:

news_grouptime a1.out


真实0m5.180s

用户0m3.296s

sys 0m0.128s

第二节目:

news_grouptime a2.out


真正的0m1.846s

用户0m1.276s

sys 0m0.036s

第三节目:

news_grouptime a3。 out


真实0m2.434s

用户0m1.656s

sys 0m0.05 6s

仍然是C ++ std :: string胜利(虽然测试并没有真正说出任何东西

,因为智能编译器可能会或多或少地完全重新选择循环)。 />
最好


Kai-Uwe Bux


arnuld发布:


i正在进行练习4.3.1 - C ++ Primer 4 / e的4.29。作者,

与运行时显示,声称C ++库字符串比

C风格字符串更快。



C ++库字符串使用动态内存分配,与替代方案相比,这是缓慢的。



< blockquote class =post_quotes>
如果我们在C ++而不是Library String类中使用C风格的字符串,那么

它们很慢



我真诚地怀疑。


但如果在C中写相同的程序则C字符串是

" ;更快"比C ++ Library字符串和& C ++中的C风格字符串。下面

是3个程序的代码。我将非常感谢有关

C ++编程的任何评论,关于练习,我的代码等。



我要重写代码。在测试它们时,请注意编译器

删除冗余语句(例如strcmp)。

-----(1)C ++中以空值终止的数组 - ---


long unsigned const times = 1UL<< 31;


#include< cstring>

char const src_lit [] ="算法是定义明确的有限集合

完成任务的明确指示。;


int main()

{

char dest [sizeof src_lit];


long unsigned i = 0;


do

{

std :: strcpy(dest,src_lit);

std :: strcmp(dest,src_lit);

}

while(times + 1!= ++ i);

}


-----(2)C ++中的库字符串-----


long unsigned const times = 1UL<< 31;


#include< string>

char const src_lit [] ="算法是定义明确的有限集合

完成任务的明确指示。;


int main()

{

std :: string const src(src_lit);


std :: string dest;


long unsigned i = 0 ;


do

{

dest = src;

dest == src;

}

while(times + 1!= ++ i);

}


- ---(3)C90中的空终止数组-----


#define TIMES(1UL<< 31)


#include< string.h>


char const src_lit [] ="算法是定义明确的有限集合

完成任务的明确指示。;


int main(无效)

{

char dest [sizeof src_lit];


长无符号i = 0;




{

strcpy(dest,src_lit);

strcmp(dest,src_lit);

}

while(TIMES + 1!= ++ i);


返回0;

}

这些测试可能会偏向于以null结尾的字符串,因为在编译时字符串的长度是已知的。然而,

std :: string的问题在于它将_always_使用动态分配

而不管编译时是否知道长度,或者是否'的已知b $ b足够小,可以容纳超大的缓冲区。


-


弗雷德里克Gotham


i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors,
with "run-time shown", claim that C++ Library strings are faster than
C-style character strings. i wrote the same programme in C & hence
found that claim of the authors is *partial*. If we use C-style strings
in C++ instead of Library String class, then they are slow but if write
the same programme in C then C strings are "faster" than both C++
Library strings & C-style strings in C++. Below is the code of 3
programmes. i will really appreciate any comments on C++ programming,
regarding exercise, my code etc.

thanks

// C-style strings in C++

#include <iostream>
#include <cstring>

int main() {
// C style character string implementation
const char *pc = "a very long literal string";
const size_t len = strlen(pc + 1); // for NULL terminator

// peformance test on string allocation and copy
for(size_t ix = 0; ix != 1000000; ++ix)
{
char *pc2 = new char[len + 1]; // dynamic space allocation with
NULL terminator
strcpy(pc2, pc); // copying string onto "allocated
space"
if(strcmp(pc2, pc))
;
delete [] pc2;
}
}
-------------------------------------------------------------------------

// C++ Library Strings
#include <iostream>
#include <string>

int main() {
// C++ standard library string implementation
std::string str("a very long literal string");

// performance tst on staring allocation & copy
// automatic memory management by String Library Class
for(unsigned long ix = 0; ix != 10000000; ++ix)
{
std::string str2 = str;
if(str == str2)
;
}

}
----------------------------------------------------------

// C strings

#include <string.h>

int main() {
char *ps = "a very long literal string";
int len = strlen(ps + 1);
unsigned long i;
char *ps2;

for(i = 0; i <= 10000000; ++i)
{
*ps2 = malloc(len + 1);
strcpy(ps2, ps);
if(strcmp(ps2, ps))
;
free(ps2);
ps2 = NULL;
}
}
-- arnuld
http://arnuld.blogspot.com

解决方案

* arnuld:

i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors,
with "run-time shown", claim that C++ Library strings are faster than
C-style character strings. i wrote the same programme in C & hence
found that claim of the authors is *partial*. If we use C-style strings
in C++ instead of Library String class, then they are slow but if write
the same programme in C then C strings are "faster" than both C++
Library strings & C-style strings in C++. Below is the code of 3
programmes. i will really appreciate any comments on C++ programming,
regarding exercise, my code etc.

thanks

// C-style strings in C++

#include <iostream>
#include <cstring>

int main() {
// C style character string implementation
const char *pc = "a very long literal string";
const size_t len = strlen(pc + 1); // for NULL terminator

Spot the bug.

// peformance test on string allocation and copy
for(size_t ix = 0; ix != 1000000; ++ix)
{
char *pc2 = new char[len + 1]; // dynamic space allocation with
NULL terminator
strcpy(pc2, pc); // copying string onto "allocated
space"
if(strcmp(pc2, pc))
;
delete [] pc2;
}
}
-------------------------------------------------------------------------

// C++ Library Strings
#include <iostream>
#include <string>

int main() {
// C++ standard library string implementation
std::string str("a very long literal string");

// performance tst on staring allocation & copy
// automatic memory management by String Library Class
for(unsigned long ix = 0; ix != 10000000; ++ix)
{
std::string str2 = str;
if(str == str2)
;
}

}
----------------------------------------------------------

// C strings

#include <string.h>

int main() {
char *ps = "a very long literal string";
int len = strlen(ps + 1);

Spot the bug.

unsigned long i;
char *ps2;

for(i = 0; i <= 10000000; ++i)

Spot the difference.

{
*ps2 = malloc(len + 1);
strcpy(ps2, ps);
if(strcmp(ps2, ps))
;
free(ps2);
ps2 = NULL;
}
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


arnuld wrote:

i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors,
with "run-time shown", claim that C++ Library strings are faster than
C-style character strings. i wrote the same programme in C & hence
found that claim of the authors is *partial*. If we use C-style strings
in C++ instead of Library String class, then they are slow but if write
the same programme in C then C strings are "faster" than both C++
Library strings & C-style strings in C++. Below is the code of 3
programmes. i will really appreciate any comments on C++ programming,
regarding exercise, my code etc.

thanks

// C-style strings in C++

#include <iostream>
#include <cstring>

int main() {
// C style character string implementation
const char *pc = "a very long literal string";
const size_t len = strlen(pc + 1); // for NULL terminator

// peformance test on string allocation and copy
for(size_t ix = 0; ix != 1000000; ++ix)

This magic number is one digit shorter than the others:

for(size_t ix = 0; ix != 10000000; ++ix)

{
char *pc2 = new char[len + 1]; // dynamic space allocation with
NULL terminator
strcpy(pc2, pc); // copying string onto "allocated
space"
if(strcmp(pc2, pc))
;
delete [] pc2;
}
}
-------------------------------------------------------------------------

// C++ Library Strings
#include <iostream>
#include <string>

int main() {
// C++ standard library string implementation
std::string str("a very long literal string");

// performance tst on staring allocation & copy
// automatic memory management by String Library Class
for(unsigned long ix = 0; ix != 10000000; ++ix)
{
std::string str2 = str;
if(str == str2)
;
}

}
----------------------------------------------------------

// C strings

#include <string.h>

#include <stdlib.h>

>
int main() {
char *ps = "a very long literal string";
int len = strlen(ps + 1);
unsigned long i;
char *ps2;

for(i = 0; i <= 10000000; ++i)
{
*ps2 = malloc(len + 1);

That should be

ps2 = malloc(len + 1);

or

ps2 = (char*) malloc(len + 1);

strcpy(ps2, ps);
if(strcmp(ps2, ps))
;
free(ps2);
ps2 = NULL;
}
}
-- arnuld
http://arnuld.blogspot.com

With the fixed programs, I get:

First program:
news_grouptime a1.out

real 0m5.180s
user 0m3.296s
sys 0m0.128s
Second program:
news_grouptime a2.out

real 0m1.846s
user 0m1.276s
sys 0m0.036s
Third program:
news_grouptime a3.out

real 0m2.434s
user 0m1.656s
sys 0m0.056s
Still C++ std::string wins (although the test does not really say anything
as a smart compiler could more or less optmize away the loop completely).
Best

Kai-Uwe Bux


arnuld posted:

i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors,
with "run-time shown", claim that C++ Library strings are faster than
C-style character strings.


C++ Library strings use dynamic memory allocation, which is sluggish by
comparison to the alternative.

If we use C-style strings in C++ instead of Library String class, then
they are slow


I sincerely doubt that.

but if write the same programme in C then C strings are
"faster" than both C++ Library strings & C-style strings in C++. Below
is the code of 3 programmes. i will really appreciate any comments on
C++ programming, regarding exercise, my code etc.


I''m going to rewrite the code. When testing them, watch out for the compiler
removing redundant statements (such as strcmp).
----- (1) Null-terminated arrays in C++ -----

long unsigned const times = 1UL << 31;

#include <cstring>

char const src_lit[] = "An algorithm is a finite set of well-defined "
"unambiguous instructions for accomplishing a task.";

int main()
{
char dest[sizeof src_lit];

long unsigned i = 0;

do
{
std::strcpy(dest,src_lit);
std::strcmp(dest,src_lit);
}
while(times+1 != ++i);
}

----- (2) Library strings in C++ -----

long unsigned const times = 1UL << 31;

#include <string>

char const src_lit[] = "An algorithm is a finite set of well-defined "
"unambiguous instructions for accomplishing a task.";

int main()
{
std::string const src(src_lit);

std::string dest;

long unsigned i = 0;

do
{
dest = src;
dest == src;
}
while(times+1 != ++i);
}

----- (3) Null-terminated arrays in C90 -----

#define TIMES (1UL << 31)

#include <string.h>

char const src_lit[] = "An algorithm is a finite set of well-defined "
"unambiguous instructions for accomplishing a task.";

int main(void)
{
char dest[sizeof src_lit];

long unsigned i = 0;

do
{
strcpy(dest,src_lit);
strcmp(dest,src_lit);
}
while(TIMES+1 != ++i);

return 0;
}
These tests might be slighly biased toward null-terminated strings, given
that the length of the string is known at compile-time. The problem with
std::string, however, is that it will _always_ use dynamic allocation
irrespective of whether the length is known at compile-time, or whether it''s
known to be small enough to allow a over-sized buffer.

--

Frederick Gotham


这篇关于C ++字符串&amp; C字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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