记录字符串...... [英] Log strings...

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

问题描述

我做了一些日志记录:


Log.Error(未找到歌曲。艺术家 - " +艺术家+,标题 - " +

title +" \ n" + exception.ToString());


现在我知道以这种方式构建字符串效率不高,但使用

字符串构建器对于这些来说似乎太复杂。


StringBuilder b = new StringBuilder();

b.Append(" ;这首歌没有找到。艺术家 - ");

b.Append(艺术家);

b.Append(",Title - ");

b.Append(title);

b.Append(" \ n");

b.Append(exception.ToString( ));


Log.Error(b.ToString());


是的,这应该更有效率,但看着这个烂摊子只有

*有*是更好的方式,那么它是什么?


谢谢,

Nick Z.

解决方案

尼克,


实际上,这将是我很快这可以归结为对

Concat的调用。 Concat将遍历字符串,计算字符串的最终长度

。然后,它会将字符串复制到新字符串。在这种情况下,
实际上可能击败了StringBuilder(因为StringBuilder

会分配)。不是很多(可能)。


对于单个连接,Concat(或+)是要走的路。对于事物

在循环中你将追加到前一个结果,StringBuilder

更好。


希望这有助于。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Nick Z." < PA ***** @ gmail.com>在消息中写道

新闻:Vn ***************** @ fe08.lga ...

我做了一些记录像这样:

Log.Error(未找到歌曲。艺术家 - &+; +艺术家+,标题 - &#; +
标题+" \ n" + exception.ToString());

现在我知道以这种方式构建字符串是不够的,但是使用字符串构建器对于这些来说似乎太复杂了。

StringBuilder b = new StringBuilder();
b.Append(找不到歌曲。艺术家 - );
b.Append(艺术家);
b.Append (",Title - ");
b.Append(title);
b.Append(" \ n");
b.Append(exception.ToString() );

Log.Error(b.ToString());

是的,这应该更有效率,但看着这个烂摊子只有
*有*什么是更好的方式呢?

谢谢,
Nick Z。



啊,现在这一切都有道理,谢谢。

我知道编译器必须比在

时间执行这个字符串更聪明,但是每个人都在说,使用StringBuilder,这就是

混乱开始的地方。 br />

再次感谢。

Nick Z.


Nicholas Paldino [.NET / C#MVP]写道:

尼克,

实际上,这将非常快。这可以归结为对Concat的调用。 Concat将遍历字符串,计算字符串的最终长度。然后,它会将字符串复制到新字符串。在这种情况下,它可能实际上击败了StringBuilder(因为StringBuilder将会进行全面分配)。不是很多(可能)。

对于单个连接,Concat(或+)是要走的路。对于你将要附加到上一个结果的循环中的东西,StringBuilder
更好。

希望这会有所帮助。



Nick,


是的,这方面通常会出现混乱。


基本上,一个拍摄操作,其结果不是下一个连接操作中的一个因素,Concat / +路线是最好的方法。

例如:


//将N个字符串组合在一起

public void Write(string str1,string str2,string str3)

{

Console.Writeline(str1 + str2 + str3);

}


当你处于紧密循环中,并且你正在使用结果下一个操作的

连接(假设你是连接来自

a数据集的值),那么字符串构建器就更好了。例如:


//获取逗号分隔的值列表数据集中的列。

公共字符串CommaDelimitedString(DataTable表)

{

//结果。

StringBuilder result = new StringBuilder();


//通过表枚举。

foreach(表中的DataRow行)

{

//附加值和逗号。

result.AppendFormat(" {0},",row [" value"]);

}


//删除最后一个逗号,如果构建器中有项目。

if(result.Length> 0)

{

//删除最后一个逗号。

result.Remove(result.Length - 1,1);

}


//返回结果。

返回result.ToString();

}

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Nick Z." < PA ***** @ gmail.com>在留言中写道

新闻:AC **************** @ fe08.lga ...

啊,现在全部是有道理的,谢谢。
我知道编译器必须比在一个字符串上更聪明,但每个人都在说,使用StringBuilder,这就是
混乱开始的地方。 />
再次感谢。
Nick Z.

Nicholas Paldino [.NET / C#MVP]写道:

Nick,

实际上,这将非常快。这可以归结为对Concat的调用。 Concat将遍历字符串,计算字符串的最终长度。然后,它会将字符串复制到新的
字符串。在这种情况下,它可能实际上击败了StringBuilder(因为StringBuilder会进行全局分配)。不是很多(可能)。

对于单个连接,Concat(或+)是要走的路。对于你将要附加到前一个结果的循环中的东西,
StringBuilder更好。

希望这会有所帮助。



I do some logging like so:

Log.Error("The song was not found. Artist - " + artist + ", Title - " +
title + "\n" + exception.ToString());

Now I know that building strings in that way is innefficient, but using
the String builder seems too complicated for the taks.

StringBuilder b = new StringBuilder();
b.Append("The song was not found. Artist - ");
b.Append(artist);
b.Append(", Title - ");
b.Append(title);
b.Append("\n");
b.Append(exception.ToString());

Log.Error(b.ToString());

Yea this should be more efficient, but looking at this mess there just
*has* to be a better way, so what is it?

Thanks,
Nick Z.

解决方案

Nick,

Actually, this will be pretty quick. This compiles down to a call to
Concat. Concat will cycle through the strings, calculating the final length
of the string. Then, it will copy the strings over to the new string. It
might actually beat out StringBuilder in this case (since StringBuilder
would overallocate). Not by much (probably).

For single concatenations, Concat (or +) is the way to go. For things
in a loop where you will be appending to the previous result, StringBuilder
is better.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nick Z." <pa*****@gmail.com> wrote in message
news:Vn*****************@fe08.lga...

I do some logging like so:

Log.Error("The song was not found. Artist - " + artist + ", Title - " +
title + "\n" + exception.ToString());

Now I know that building strings in that way is innefficient, but using
the String builder seems too complicated for the taks.

StringBuilder b = new StringBuilder();
b.Append("The song was not found. Artist - ");
b.Append(artist);
b.Append(", Title - ");
b.Append(title);
b.Append("\n");
b.Append(exception.ToString());

Log.Error(b.ToString());

Yea this should be more efficient, but looking at this mess there just
*has* to be a better way, so what is it?

Thanks,
Nick Z.



Ah, now it all makes sense, thanks.
I knew the compiler has to be smarter than doing this one string at a
time, but everybody keeps saying, use StringBuilder, thats where the
confusion started.

Thanks again.
Nick Z.

Nicholas Paldino [.NET/C# MVP] wrote:

Nick,

Actually, this will be pretty quick. This compiles down to a call to
Concat. Concat will cycle through the strings, calculating the final length
of the string. Then, it will copy the strings over to the new string. It
might actually beat out StringBuilder in this case (since StringBuilder
would overallocate). Not by much (probably).

For single concatenations, Concat (or +) is the way to go. For things
in a loop where you will be appending to the previous result, StringBuilder
is better.

Hope this helps.



Nick,

Yes, there typically tends to be confusion in this area.

Basically, for one shot operations, where the outcome is not a factor in
the next concatenation operation, the Concat/+ route is the best way to go.
For example:

// Add N strings together
public void Write(string str1, string str2, string str3)
{
Console.Writeline(str1 + str2 + str3);
}

When you are in a tight loop, and you are using the result of the
concatenation for the next operation (say you are concatenating values from
a dataset), then the string builder is better. For example:

// Get a comma delimited list of the "value" column in the dataset.
public string CommaDelimitedString(DataTable table)
{
// The result.
StringBuilder result = new StringBuilder();

// Enumerate through the table.
foreach (DataRow row in table)
{
// Append the value, and a comma.
result.AppendFormat("{0},", row["value"]);
}

// Remove the last comma, if there are items in the builder.
if (result.Length > 0)
{
// Remove the last comma.
result.Remove(result.Length - 1, 1);
}

// Return the result.
return result.ToString();
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nick Z." <pa*****@gmail.com> wrote in message
news:AC****************@fe08.lga...

Ah, now it all makes sense, thanks.
I knew the compiler has to be smarter than doing this one string at a
time, but everybody keeps saying, use StringBuilder, thats where the
confusion started.

Thanks again.
Nick Z.

Nicholas Paldino [.NET/C# MVP] wrote:

Nick,

Actually, this will be pretty quick. This compiles down to a call to
Concat. Concat will cycle through the strings, calculating the final
length of the string. Then, it will copy the strings over to the new
string. It might actually beat out StringBuilder in this case (since
StringBuilder would overallocate). Not by much (probably).

For single concatenations, Concat (or +) is the way to go. For
things in a loop where you will be appending to the previous result,
StringBuilder is better.

Hope this helps.



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

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