简体   繁体   English

冗余对象.ToString

[英]Redundant Object.ToString

I am reviewing the following code.我正在查看以下代码。

private string MakeKey(int SnapshotID, string UserName, string BrandName)
{
    return string.Format("{0}:{1}:{2}", SnapshotID.ToString(), UserName, BrandName);
}

The SnapshotID.ToString() redundantly calls the ToString() Method. SnapshotID.ToString()冗余调用ToString()方法。
What are the potential issues that could occur?可能发生的潜在问题是什么? eg speed.例如速度。

EDIT编辑
I have added a test if anyone wants to refine: http://pastebin.com/gynNjwT1如果有人想改进,我已经添加了一个测试:http: //pastebin.com/gynNjwT1

With ToString:      0.010884
Without ToString:   0.001446

With ToString:      0.005506
Without ToString:   0.002852

With ToString:      0.001155
Without ToString:   0.009117

With ToString:      0.003210
Without ToString:   0.001546

In this code...no, it's just redundant and even slightly slower because String.Format() will call again ToString() on the string itself.在这段代码中......不,它只是多余的,甚至有点慢,因为String.Format()将在string本身上再次调用ToString()

In case String.Format() is used with an IFormatProvider an explicit ToString() will override it.如果String.Format()IFormatProvider一起使用,则显式ToString()将覆盖它。

Edits (from comments): note about performance.编辑(来自评论):关于性能的注释。 It may be or not slower (an useless ToString() call) compared to primitive type ( int ) boxing.与原始类型( int )装箱相比,它可能会或不会更慢(无用ToString()调用)。 We should measure this but it's pretty hard to say.我们应该衡量这个,但很难说。 What's slower?什么更慢? A virtual function call or a boxing ?虚函数调用还是拳击 Does it really matter when compared to String.Format() total time?String.Format()总时间相比,这真的很重要吗? It has been already answered here on SO . 已经在这里回答了 SO IMO if it has been done as a smart optimization it's pretty useless. IMO,如果它是作为一种智能优化完成的,那它就毫无用处了。

Performance表现

OP made a small test for this (see comments), just for fun , results on my test machine: OP为此做了一个小测试(见评论),只是为了好玩,我的测试机器上的结果:

Test               Time [ms]
With ToString      0.002929
Without ToString:  0.003414

I rewrote test to do not use threads (because it'll add more variables, cores may differ and OS will load them dynamically).我重写了不使用线程的测试(因为它会添加更多变量,内核可能会有所不同,操作系统会动态加载它们)。 With this (over simple!) test code (derived from OP test):使用这个(非常简单!)测试代码(源自 OP 测试):

Stopwatch sw = new Stopwatch();
sw.Start();
for (int x = 0; x < Count; x++)
{
    _temp = MakeKeyToString(1, "Ashley", "MyBrand");
}
sw.Stop();
TimeSpan test = TimeSpan.FromMilliseconds((double)sw.ElapsedMilliseconds);

I get that ToString() version is faster (for Count = 1000000 ) in average around 10/20 nanoseconds.我知道ToString()版本更快(对于Count = 1000000 )平均大约 10/20 纳秒。 IMO to measure something so small we need a much better test environment and a more professional approach. IMO 要测量这么小的东西,我们需要更好的测试环境和更专业的方法。 Changing his code for String.Format() to use IFormatProvider :将他的String.Format()代码更改为使用IFormatProvider

string MakeKeyToString(int SnapshotID, string UserName, string BrandName)
{
    return string.Format(CultureInfo.InvariantCulture,
        "{0}:{1}:{2}", SnapshotID.ToString(), UserName, BrandName);
}

Change everything again: without ToString() is faster for 200 nanoseconds.再次更改所有内容:没有ToString()会更快 200 纳秒。 Again too small to measure in this way.同样太小而无法以这种方式测量。

Conclusions结论

You can't even start to consider this an optimization, too many factors will play around that and it's so small that will go unnoticed compared to total String.Format() time.你甚至不能开始考虑这是一种优化,太多的因素会围绕它发挥作用,而且与String.Format()的总时间相比,它是如此之小以至于不会被注意到。 What's worse it may introduce subtle bugs if you'll change String.Format() to use an IFormatProvider because it'll make you stop and think "Why this? There should be a culture-related reason. Maybe..." when in reality...(probably) there is not.更糟糕的是,如果您将String.Format()更改为使用IFormatProvider ,它可能会引入细微的错误,因为它会让您停下来思考“为什么会这样?应该有与文化相关的原因。也许......”现实......(可能)没有。

In this example, by calling Int32.ToString() you are avoiding the overhead of boxing your int into an Object .在此示例中,通过调用Int32.ToString()您可以避免将int装箱到Object中的开销。

However the overhead of calling .ToString() , then having String.Format call .ToString() on that may outweight the overhead of the boxing.然而,调用.ToString()的开销,然后让String.Format调用.ToString()的开销可能超过装箱的开销。 And all of it is negligible in respect to String.Format .String.Format而言,所有这些都可以忽略不计。

Nope, The code is redundant and stop.不,代码是多余的并停止。 Also SnapshotID cannot be null since it is an integer and not a nullable integer ( int? ). SnapshotID也不能为空,因为它是整数而不是可为空的整数 ( int? )。 In this case the ToString method will be called twice I think (one explicitly and one on the result of the first call) so maybe some performance differences can be faced.在这种情况下,我认为ToString方法将被调用两次(一次显式调用,一次调用第一次调用的结果),因此可能会面临一些性能差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM