std :: string和std :: ostringstream表演 [英] std::string and std::ostringstream performances

查看:76
本文介绍了std :: string和std :: ostringstream表演的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个C ++应用程序,广泛使用std :: string和

std :: ostringstream,其方式与下面的方式类似/>

std :: string msgHeader;


msgHeader ="<" ;;

msgHeader + = a;

msgHeader + ="><";


msgHeader + = b;

msgHeader + =" ><" ;;


msgHeader + = c;

msgHeader + =">" ;;


同样它也使用ostringstream和使用

的函数几乎在我的应用程序上的每条消息上调用

套接字。我正在使用它来精确地构建一个XML消息,以便将
发送到另一个应用程序。


我们在应用程序上运行collect / analyzer时观察到的内容是
,它显示了大部分CPU用于处理这些2

数据类型,使用std :: allocator和其他的内存分配

东西。 CPU有时会高达100%。


我想就以下几点获得建议/建议

1.有没有更好的方法使用std :: string / std :: ostringstream而不是

我一直在使用它的方式?

2. AM我使用错误的数据类型进行此类操作

应继续使用其他东西吗?有什么建议

数据类型应该是什么?


我最终需要这些数据类型,因为我使用的外部库是
发送此数据需要它在std :: string /

std :: ostringstream格式。


想要一些建议来降低CPU利用率。


谢谢,

Bala

Hi,

I have a C++ application that extensively uses std::string and
std::ostringstream in somewhat similar manner as below

std::string msgHeader;

msgHeader = "<";
msgHeader += a;
msgHeader += "><";

msgHeader += b;
msgHeader += "><";

msgHeader += c;
msgHeader += ">";

Similarly it uses ostringstream as well and the function that uses
this gets called almost on every message that my application gets on
the socket. I am using this to precisely construct a XML Message to
be sent to another application.

What we observed when we ran a collect/analyzer on the application is
that it shows majority of the CPU spent in trying to deal with these 2
datatypes, their memory allocation using std::allocator and other
stuff. The CPU goes as high as 100% sometimes.

I would like to get an advice/suggestion on the following points
1. Is there a better way to use std::string / std::ostringstream than
the way I have been using it?
2. AM I using the wrong datatype for such kind of operations and
should move on to use something else? Any suggestions what the
datatype should be?

I eventually need these datatypes because the external library that I
am using to send this data out needs it in std::string /
std::ostringstream formats.

Would like to have some suggestions to bring down the CPU utilization.

Thanks,
Bala

推荐答案

" ; Bala2508" < R。*********** @ gmail.com写信息

新闻:11 ****************** ****@v3g2000hsg.googlegro ups.com ...
"Bala2508" <R.***********@gmail.comwrote in message
news:11**********************@v3g2000hsg.googlegro ups.com...




我有一个C ++应用程序,广泛使用std :: string和

std :: ostringstream,方式与下面的方式类似。


std :: string msgHeader;


msgHeader ="<" ;;

msgHeader + = a;

msgHeader + ="><" ;;


msgHeader + = b;

msgHeader + ="><";


msgHeader + = c ;

msgHeader + =">" ;;


同样它也使用ostringstream和使用

的函数几乎在我的应用程序上的每条消息上都会调用它。

套接字。我正在使用它来精确地构建一个XML消息,以便将
发送到另一个应用程序。


我们在应用程序上运行collect / analyzer时观察到的内容是
,它显示了大部分CPU用于处理这些2

数据类型,使用std :: allocator和其他的内存分配

东西。 CPU有时会高达100%。


我想就以下几点获得建议/建议

1.有没有更好的方法使用std :: string / std :: ostringstream而不是

我一直在使用它的方式?

2. AM我使用错误的数据类型进行此类操作

应继续使用其他东西吗?有什么建议

数据类型应该是什么?


我最终需要这些数据类型,因为我使用的外部库是
发送此数据需要它在std :: string /

std :: ostringstream格式。


想要一些建议来降低CPU利用率。
Hi,

I have a C++ application that extensively uses std::string and
std::ostringstream in somewhat similar manner as below

std::string msgHeader;

msgHeader = "<";
msgHeader += a;
msgHeader += "><";

msgHeader += b;
msgHeader += "><";

msgHeader += c;
msgHeader += ">";

Similarly it uses ostringstream as well and the function that uses
this gets called almost on every message that my application gets on
the socket. I am using this to precisely construct a XML Message to
be sent to another application.

What we observed when we ran a collect/analyzer on the application is
that it shows majority of the CPU spent in trying to deal with these 2
datatypes, their memory allocation using std::allocator and other
stuff. The CPU goes as high as 100% sometimes.

I would like to get an advice/suggestion on the following points
1. Is there a better way to use std::string / std::ostringstream than
the way I have been using it?
2. AM I using the wrong datatype for such kind of operations and
should move on to use something else? Any suggestions what the
datatype should be?

I eventually need these datatypes because the external library that I
am using to send this data out needs it in std::string /
std::ostringstream formats.

Would like to have some suggestions to bring down the CPU utilization.



一个建议是.reserve()。我E.

std :: string msgHeader;

msgHeader.reserve(100);


这样字符串msgHeader就不会在使用分配的最初100个字符之前,不需要尝试分配更多内存

。有些编译器在预分配默认字节数时比其他编译器好。
。有时

他们必须给出一个暗示。找出一个好的尺寸来预留(一个很大的b
,你不需要做重新分配,一个小到足够的

你还没有用完记忆)然后尝试再次分析它,看看它是否有帮助。

One suggestion would be .reserve(). I E.
std::string msgHeader;
msgHeader.reserve( 100 );

That way the string msgHeader wouldn''t need to try to allocate more memory
until it has used the initial 100 characters allocated. Some compilers are
better at preallocating a default number of bytes than others. Sometimes
they have to be given a hint. Figure out a good size to reserve (one big
enough where you won''t need to be doing reallocatings, one small enough that
you''re not running out of memory) and then try profiling it again and see if
it helps.


10月31日,下午12:09,吉姆兰斯顿" < tazmas ... @ rocketmail.comwrote:
On Oct 31, 12:09 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:

" Bala2508" < R.Balaji.I ... @ gmail.com写信息


新闻:11 ******************* ***@v3g2000hsg.googlegro ups.com ...

"Bala2508" <R.Balaji.I...@gmail.comwrote in message

news:11**********************@v3g2000hsg.googlegro ups.com...




Hi,


我有一个C ++应用程序,广泛使用std :: string和

std :: ostringstream,其方式与
I have a C++ application that extensively uses std::string and
std::ostringstream in somewhat similar manner as below


std :: string msgHeader;
std::string msgHeader;


msgHeader ="<" ;;

msgHeader + = a;

msgHeader + ="><" ;;
msgHeader = "<";
msgHeader += a;
msgHeader += "><";


msgHeader + = b;

msgHeader + ="><" ;;
msgHeader += b;
msgHeader += "><";


msgHeader + = c;

msgHeader + =">" ;;
msgHeader += c;
msgHeader += ">";


同样它也使用ostringstream和使用

的函数几乎在我的应用程序得到的每条消息上调用它在

套接字上。我正在使用它来精确地构造一个XML消息,以便将
发送到另一个应用程序。
Similarly it uses ostringstream as well and the function that uses
this gets called almost on every message that my application gets on
the socket. I am using this to precisely construct a XML Message to
be sent to another application.


我们在应用程序上运行收集器/分析器时观察到的是

它显示了大部分CPU花费在尝试处理这些2

数据类型时,他们的内存分配使用std :: allocator和其他

的东西。 CPU有时会高达100%。
What we observed when we ran a collect/analyzer on the application is
that it shows majority of the CPU spent in trying to deal with these 2
datatypes, their memory allocation using std::allocator and other
stuff. The CPU goes as high as 100% sometimes.


我想就以下几点获得建议/建议

1.有没有更好的使用方法std :: string / std :: ostringstream比

我一直在使用它的方式?

2. AM我使用错误的数据类型进行此类操作并且

应继续使用其他东西吗?有什么建议

数据类型应该是什么?
I would like to get an advice/suggestion on the following points
1. Is there a better way to use std::string / std::ostringstream than
the way I have been using it?
2. AM I using the wrong datatype for such kind of operations and
should move on to use something else? Any suggestions what the
datatype should be?


我最终需要这些数据类型,因为我用b / b
用于发送此数据的外部库需要它在std中:: string /

std :: ostringstream格式。
I eventually need these datatypes because the external library that I
am using to send this data out needs it in std::string /
std::ostringstream formats.


想要一些降低CPU利用率的建议。
Would like to have some suggestions to bring down the CPU utilization.



一个建议是.reserve()。我E.

std :: string msgHeader;

msgHeader.reserve(100);


这样字符串msgHeader就不会在使用分配的最初100个字符之前,不需要尝试分配更多内存

。有些编译器在预分配默认字节数时比其他编译器好。
。有时

他们必须给出一个暗示。找出一个好的尺寸来预留(一个很大的b
,你不需要做重新分配,一个小到足够的

你还没有用完记忆)然后尝试再次分析它,看看它是否有帮助.-隐藏引用的文字 -


- 显示引用的文字 -


One suggestion would be .reserve(). I E.
std::string msgHeader;
msgHeader.reserve( 100 );

That way the string msgHeader wouldn''t need to try to allocate more memory
until it has used the initial 100 characters allocated. Some compilers are
better at preallocating a default number of bytes than others. Sometimes
they have to be given a hint. Figure out a good size to reserve (one big
enough where you won''t need to be doing reallocatings, one small enough that
you''re not running out of memory) and then try profiling it again and see if
it helps.- Hide quoted text -

- Show quoted text -



一旦完成,我也会使用msgHeader.str("")方法清除字符串

并发送消息。然后再次调用此方法调用
时,会发生相同的事件序列。一旦我做了一个msgHeader.str("),它是否会清除

分配的内存?如何保留

在这种情况下基本上有所帮助?

I also clear the string using msgHeader.str("") method once i am done
with the sending of the message. Then again when this method gets
called, the same sequence of events happen. Wouldnt it clear the
allocated memory once i do a msgHeader.str("")? How do reserving
essentially help in this scenario?


在2007-10-31 19:48,Bala写道:
On 2007-10-31 19:48, Bala wrote:

10月31日下午12:09,Jim Langston < tazmas ... @ rocketmail.comwrote:
On Oct 31, 12:09 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:

>" Bala2508" < R.Balaji.I ... @ gmail.com写信息

新闻:11 ********************** @ v3g2000hsg.googlegr oups.com ......

>"Bala2508" <R.Balaji.I...@gmail.comwrote in message

news:11**********************@v3g2000hsg.googlegr oups.com...




Hi,


我有一个C ++应用程序,广泛使用std :: string和

std :: ostringstream,其方式与
I have a C++ application that extensively uses std::string and
std::ostringstream in somewhat similar manner as below


std :: string msgHeader;
std::string msgHeader;


msgHeader ="<" ;;

msgHeader + = a;

msgHeader + ="><" ;;
msgHeader = "<";
msgHeader += a;
msgHeader += "><";


msgHeader + = b;

msgHeader + ="><" ;;
msgHeader += b;
msgHeader += "><";


msgHeader + = c;

msgHeader + =">" ;;
msgHeader += c;
msgHeader += ">";


同样它也使用ostringstream和使用

的函数几乎在我的应用程序得到的每条消息上调用它在

套接字上。我正在使用它来精确地构造一个XML消息,以便将
发送到另一个应用程序。
Similarly it uses ostringstream as well and the function that uses
this gets called almost on every message that my application gets on
the socket. I am using this to precisely construct a XML Message to
be sent to another application.


我们在应用程序上运行收集器/分析器时观察到的是

它显示了大部分CPU花费在尝试处理这些2

数据类型时,他们的内存分配使用std :: allocator和其他

的东西。 CPU有时会高达100%。
What we observed when we ran a collect/analyzer on the application is
that it shows majority of the CPU spent in trying to deal with these 2
datatypes, their memory allocation using std::allocator and other
stuff. The CPU goes as high as 100% sometimes.


我想就以下几点获得建议/建议

1.有没有更好的使用方法std :: string / std :: ostringstream比

我一直在使用它的方式?

2. AM我使用错误的数据类型进行此类操作并且

应继续使用其他东西吗?有什么建议

数据类型应该是什么?
I would like to get an advice/suggestion on the following points
1. Is there a better way to use std::string / std::ostringstream than
the way I have been using it?
2. AM I using the wrong datatype for such kind of operations and
should move on to use something else? Any suggestions what the
datatype should be?


我最终需要这些数据类型,因为我用b / b
用于发送此数据的外部库需要它在std中:: string /

std :: ostringstream格式。
I eventually need these datatypes because the external library that I
am using to send this data out needs it in std::string /
std::ostringstream formats.


想要一些降低CPU利用率的建议。
Would like to have some suggestions to bring down the CPU utilization.


一个建议是.reserve()。我是......
std :: string msgHeader;
msgHeader.reserve(100);

这样字符串msgHeader就不需要尝试分配更多内存了/>直到它使用了分配的最初100个字符。有些编译器在预分配默认字节数方面比其他编译器更好。有时他们必须给出一个暗示。找出一个好的大小来保留(一个很大的
,你不需要做重新分配,一个小到足以让你没有内存不足),然后尝试分析它再次看看它是否有帮助.-隐藏引用文本 -


One suggestion would be .reserve(). I E.
std::string msgHeader;
msgHeader.reserve( 100 );

That way the string msgHeader wouldn''t need to try to allocate more memory
until it has used the initial 100 characters allocated. Some compilers are
better at preallocating a default number of bytes than others. Sometimes
they have to be given a hint. Figure out a good size to reserve (one big
enough where you won''t need to be doing reallocatings, one small enough that
you''re not running out of memory) and then try profiling it again and see if
it helps.- Hide quoted text -



我也使用msgHeader.str("")方法清除字符串我完成了

发送消息。然后再次调用此方法调用
时,会发生相同的事件序列。一旦我做了一个msgHeader.str("),它是否会清除

分配的内存?在这种情况下,如何预留

实质上有帮助?

I also clear the string using msgHeader.str("") method once i am done
with the sending of the message. Then again when this method gets
called, the same sequence of events happen. Wouldnt it clear the
allocated memory once i do a msgHeader.str("")? How do reserving
essentially help in this scenario?



要清除字符串,请改用clear(),这就是它的意思。

clear()不会影响容量这个字符串所以如果你这样做的话就像


std :: string str;

str.reserve(100);

str.clear();


您仍然可以在字符串中放入100个字符

需要重新分配。


当然,如果在被调用的函数中声明了msgHeader,那么当函数返回时,
将超出范围并将被重新分配

再次调用它时,在这种情况下会构造一个新的字符串

在这种情况下对字符串的操作将不会影响两个

不同的电话。另一方面,如果msgHeader在

函数的外部,那么你可能会从使用reserve获益。顺便说一下,当

调用reserve()时,参数小于或等于

当前容量,不采取任何行动。


-

Erik Wikstr ?? m

To clear the string use clear() instead, that is what it is meant for.
clear() will not affect the capacity of the string so if you do
something like

std::string str;
str.reserve(100);
str.clear();

you will still be able to put 100 characters into the string before it
needs to reallocate.

Of course, if msgHeader is declared in the function that gets called it
will go out of scope when the function returns and will be reallocated
when it is called again, in which case a new string will be constructed
in which case the operations on the string will have not effect over two
different calls. If msgHeader on the other hand is external to the
function then you will probably benefit from using reserve. BTW, when
calling reserve() with an argument that is smaller than or equal to the
current capacity no action is taken.

--
Erik Wikstr??m


这篇关于std :: string和std :: ostringstream表演的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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