Object redundant.ToString
source link: https://www.codesd.com/item/object-redundant-tostring.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Object redundant.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.
What are the potential issues that could occur? e.g. speed.
EDIT
I have added a test if anyone wants to refine: 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.
In case String.Format()
is used with an IFormatProvider
an explicit ToString()
will override it.
Edits (from comments): note about performance. It may be or not slower (an useless ToString()
call) compared to primitive type (int
) boxing. 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? It has been already answered here on SO. IMO if it has been done as a smart optimization it's pretty useless.
Performance
OP made a small test for this (see comments), just for fun, results on my test machine:
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):
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. IMO to measure something so small we need a much better test environment and a more professional approach. Changing his code for String.Format()
to use 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. 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. 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.
Recommend
-
29
-
28
Previous post Every object in Java has a toString() method which ca...
-
19
1. 概述 今天在写代码的过程都遇到了一个提示: redundant 'object.tostring()' call 截图如下: I...
-
24
戳蓝字「TopCoder 」关注我们哦!
-
26
戳蓝字「TopCoder 」关注我们哦! 试想一个问题:如果我们已知Java对象的toString格式,...
-
4
Redundant 'object.tostring()' call 引发的思考 今天在写代码的过程都遇到了一个提示:redundant 'object.tostring()' call截图如下:
-
8
How to call ToString() on a discriminated union case Let's look at some ways that we can call the ToString() metho...
-
11
Introducing tostring.org : AIR / Flash Player / RIA books online Monday, June 23, 2008 Prior to the release of Adobe AIR 1.0, I worked on two books about Adobe AIR (then know by its code name of “Apollo”). The books, “Adobe...
-
6
Using ObjectUtil.toString() to trace properties of an Object Wednesday, April 12, 2006 If you need to get a quick view into an Object or class instance, you can use the
-
7
JS数据类型检测typeof、instanceof、constructor、Object.prototype.toString.call(val)的区别
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK