关于“清算”的基本问题一串...... [英] basic question about "clearing" a string....

查看:80
本文介绍了关于“清算”的基本问题一串......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个定期使用std :: string变量的应用程序

分配了一个非常非常大的字符串(15000000+字节)这个应用程序本质上是一个守护进程,并且它轮询了一个数据集

,它可以有很多信息并且在这个连接中。

单字符串变量对象。当守护进程完成其工作时,它会进入睡眠状态,但在此之前,它会清除。这个变量所以它可以在下次民意调查中再次使用



目前,当我说清楚时,我对变量的所有做法都是设置

它是一个emtpy字符串(var =""),而不是因为我对std :: string的关注而调用.clear()

成员函数

的性能实际上将这个非常大的缓冲区归零。我的问题是,这是明智的吗?尽管有这么大的记录,是否建议使用.clear()

而不是将其设置为空字符串?任何潜在的

对此有何影响?我在我的应用程序上运行了valgrind,如果我只是将变量设置为空字符串,它就不会报告内存泄漏....


Avalon1178

Hi,

I have an application that periodically uses a std::string variable
which is assigned a VERY VERY large string (15000000+ bytes long).
This application is essentially a daemon, and it polls a data set
which can have a lot of information and it is concatenated in this
single string variable object. When the daemon finishes its job, it
goes to sleep, but before doing so, it "clears" this variable so it
can be reused again in the next poll.

Currently, when I say clear, all I''m doing to the variable is setting
it to an emtpy string (var = ""), rather than calling the .clear()
member function because of my concern with std::string performance of
actually zeroing out this very large buffer. My question is, is this
wise? Despite this large record, is it advised to use .clear()
regardless rather than setting it to empty string? Any potential
implications on this? I ran valgrind on my application and it doesn''t
report a memory leak if I just set the variable to empty string....

Avalon1178

推荐答案

Avalon1178写道:
Avalon1178 wrote:

[.large string in一个程序需要在某个时候被清除..]


目前,当我说清楚时,我正在做的变量就是设置

它是一个emtpy字符串(var =""),而不是调用.clear()

成员函数,因为我关注std :: string的性能

实际上将这个非常大的缓冲区归零。
[..large string in a program needs to be cleared at some point..]

Currently, when I say clear, all I''m doing to the variable is setting
it to an emtpy string (var = ""), rather than calling the .clear()
member function because of my concern with std::string performance of
actually zeroing out this very large buffer.



谁说它将缓冲区置零?你有没有看到你的实现

实际执行归零?或者是你的推测?

Who said it is zeroing the buffer? Did you see your implementation
actually performing zeroing? Or is it your speculation?


我的问题是,这是否明智?
My question is, is this
wise?



* *明智的是什么?过早优化?基于

a推测而不是测量的优化?

Is *what* wise? Premature optimisation? Optimisation based on
a speculation instead of a measurement?


尽管有这么大的记录,是否建议使用.clear()

无论是否设置为空字符串?
Despite this large record, is it advised to use .clear()
regardless rather than setting it to empty string?



在大多数情况下,它们将是相同的,或者差异不会在整个程序执行中显而易见。

In most cases they will be equivalent or the difference is not going
to be noticeable in the overall program execution.


任何潜在的

对此有何影响?
Any potential
implications on this?



嗯?

Huh?


我在我的应用程序上运行valgrind并且它没有
如果我只是将变量设置为空字符串,则
报告内存泄漏....
I ran valgrind on my application and it doesn''t
report a memory leak if I just set the variable to empty string....



好​​。现在,如果你真的需要知道性能差异,

拉出一个分析器并实际测量时间。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Good. Now, if you really need to know the performance difference,
pull out a profiler and actually measure the time.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


WTF?

你不需要漫游并批评旁边的问题来展示

off你所知道的!该死的,你写过std :: string或者其他什么东西吗?b b b b b b b we we we we we we we we we天啊!喝一些kool-

帮助冷却....


无论如何,这是我正在寻找的唯一回应:
WTF?

You don''t need to ramble on and critique the side questions to show
off what you know! Damn, did you write std::string or something that
some speculation I said ruffled your feathers? Geez! Drink some kool-
aid to cool off....

In any case, this is the only response I''m looking for:

>
>

尽管有这么大的记录,是否建议使用.clear()

而不是将其设置为空字符串?
Despite this large record, is it advised to use .clear()
regardless rather than setting it to empty string?



在大多数情况下,它们将是相同的,或者差异不会在整个程序执行中显而易见。


In most cases they will be equivalent or the difference is not going
to be noticeable in the overall program execution.




Avalon1178< Av ******** @ aol.comwrote in message ...

Avalon1178 <Av********@aol.comwrote in message ...

>

无论如何,这是我要找的唯一回复:
>
In any case, this is the only response I''m looking for:

尽管有这么大的记录,是否建议使用.clear()

而不是将其设置为空字符串?
Despite this large record, is it advised to use .clear()
regardless rather than setting it to empty string?



在大多数情况下,它们将是等效的,或者差异不会在整个程序执行中显而易见。

In most cases they will be equivalent or the difference is not going
to be noticeable in the overall program execution.



{使用std :: cout; //用于NG post // main或function

std :: string test(" This is a string。");

cout<<" test .size()"<< test.size()<< std :: endl;

cout<<" test.capacity()"<< test .capacity()<< std :: end l;

test.clear();

cout<<"&test; test.size()"< ;< test.size()<< std :: endl;

cout<<"" test.capacity()"<< test.capacity()<< ; std :: end l;

test ="" ;;

cout<<" test.size()"<< test.size ()<< std :: endl;

cout<<"&test; test.capacity()"<< test.capacity()<< std :: end l ;

std :: string()。swap(test);

cout<<" test.size()"<< test.size( )<< std :: endl;

cout<<" test.capacity()"<< test.capacity()<< std :: end l;

}

/ * - 输出 -

test.size()17

test.capacity()17
test.size()0

test.capacity()17

test.size()0

test.capacity ()17

test.size()0

test.capacity()0

* /


-

Bob R

POVrookie

{ using std::cout; // for NG post // main or function
std::string test("This is a string.");
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::end l;
test.clear();
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::end l;
test = "";
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::end l;
std::string().swap( test );
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::end l;
}
/* - output -
test.size()17
test.capacity()17
test.size()0
test.capacity()17
test.size()0
test.capacity()17
test.size()0
test.capacity()0
*/

--
Bob R
POVrookie


这篇关于关于“清算”的基本问题一串......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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