![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
It seems that in some cases its quicker to clone an existing object vs creating a new object. I have not done exstensive perf test but have found cases where it helps. Anyone have any thoughts on this? Why would this be true? Is it always true? if not then why not? Thanks, Schneider |
#3
| |||
| |||
|
|
It seems that in some cases its quicker to clone an existing object vs creating a new object. I have not done exstensive perf test but have found cases where it helps. Anyone have any thoughts on this? Why would this be true? Is it always true? if not then why not? Thanks, Schneider |
#4
| |||
| |||
|
|
Laura's answer is dead on - Cloning & Creating a new Instance are two very different things with very different use cases. In a number of projects I work on, we built the cornerstone of our algorithms on the Prototype Pattern, which is a Clone() based implementation. Cloning is great, and has a wide variety of use cases. In most cases though, cloning is going to be quite a bit slower than creating a new instance. The code for cloning is often going to look like: public object Clone() { Foo o = new Foo(); o.ListOfNames = this.ListOfNames; o.Owner = this.Owner; return o; } This is clearly slower than just creating a new instance of Foo, as more stuff is going on. As Cloning gets more complex (as it always does) it's not uncommon to use Xml or binary Serialization, or even (and this is bad!) do a database dip. Note: The above code has a subtle bug that often bites people. Both copies (old and new) point to the same instance of ListOfNames & Owner. In the case of the string (Owner) this may not be a problem, as strings are immutable, but for many other things (Lists, Dictionaries, Datasets, etc), it's a catastrophic and subtle bug. If someone goes "old.ListOfNames.Clear()" both instances are affected. Anyone know why Microsoft never made a Generic IClonable? This always seemed silly, as IClonable<T> would eliminate the cast that always happens on clone. I usually end up with two methods: public object Clone(){} public MyType CloneAsMyType() { return (MyType) Clone());} -- Chris Mullins "schneider" <eschneider (AT) ooooooooo (DOT) com> wrote in message news:emvRHGLBIHA.3716 (AT) TK2MSFTNGP03 (DOT) phx.gbl... It seems that in some cases its quicker to clone an existing object vs creating a new object. I have not done exstensive perf test but have found cases where it helps. Anyone have any thoughts on this? Why would this be true? Is it always true? if not then why not? Thanks, Schneider |
#5
| |||
| |||
|
|
I understand your points. assuming I wanted the same object with no state, would it be faster to clone vs create? I guess i'm wondering if it does any optimization because it knows more about what the end goal is? Like it has a good idea of memory required, security. "Chris Mullins [MVP - C#]" <cmullins (AT) yahoo (DOT) com> wrote in message news:OtKtfrRBIHA.3916 (AT) TK2MSFTNGP02 (DOT) phx.gbl... Laura's answer is dead on - Cloning & Creating a new Instance are two very different things with very different use cases. In a number of projects I work on, we built the cornerstone of our algorithms on the Prototype Pattern, which is a Clone() based implementation. Cloning is great, and has a wide variety of use cases. In most cases though, cloning is going to be quite a bit slower than creating a new instance. The code for cloning is often going to look like: public object Clone() { Foo o = new Foo(); o.ListOfNames = this.ListOfNames; o.Owner = this.Owner; return o; } This is clearly slower than just creating a new instance of Foo, as more stuff is going on. As Cloning gets more complex (as it always does) it's not uncommon to use Xml or binary Serialization, or even (and this is bad!) do a database dip. Note: The above code has a subtle bug that often bites people. Both copies (old and new) point to the same instance of ListOfNames & Owner. In the case of the string (Owner) this may not be a problem, as strings are immutable, but for many other things (Lists, Dictionaries, Datasets, etc), it's a catastrophic and subtle bug. If someone goes "old.ListOfNames.Clear()" both instances are affected. Anyone know why Microsoft never made a Generic IClonable? This always seemed silly, as IClonable<T> would eliminate the cast that always happens on clone. I usually end up with two methods: public object Clone(){} public MyType CloneAsMyType() { return (MyType) Clone());} -- Chris Mullins "schneider" <eschneider (AT) ooooooooo (DOT) com> wrote in message news:emvRHGLBIHA.3716 (AT) TK2MSFTNGP03 (DOT) phx.gbl... It seems that in some cases its quicker to clone an existing object vs creating a new object. I have not done exstensive perf test but have found cases where it helps. Anyone have any thoughts on this? Why would this be true? Is it always true? if not then why not? Thanks, Schneider |
#6
| |||
| |||
|
|
If you want the same object with no state, I would go with "new" every time. Often the right high level option is to use a Factory Pattern, which abstracts this from your code: MyObject o = MyObjectFactory.CreateMyObject(); That way you can implement the creation logic any which way. -- Chris Mullins "schneider" <eschneider.news.ms (AT) starkinvestments (DOT) com> wrote in message news:eZt$99RBIHA.324 (AT) TK2MSFTNGP04 (DOT) phx.gbl... I understand your points. assuming I wanted the same object with no state, would it be faster to clone vs create? I guess i'm wondering if it does any optimization because it knows more about what the end goal is? Like it has a good idea of memory required, security. "Chris Mullins [MVP - C#]" <cmullins (AT) yahoo (DOT) com> wrote in message news:OtKtfrRBIHA.3916 (AT) TK2MSFTNGP02 (DOT) phx.gbl... Laura's answer is dead on - Cloning & Creating a new Instance are two very different things with very different use cases. In a number of projects I work on, we built the cornerstone of our algorithms on the Prototype Pattern, which is a Clone() based implementation. Cloning is great, and has a wide variety of use cases. In most cases though, cloning is going to be quite a bit slower than creating a new instance. The code for cloning is often going to look like: public object Clone() { Foo o = new Foo(); o.ListOfNames = this.ListOfNames; o.Owner = this.Owner; return o; } This is clearly slower than just creating a new instance of Foo, as more stuff is going on. As Cloning gets more complex (as it always does) it's not uncommon to use Xml or binary Serialization, or even (and this is bad!) do a database dip. Note: The above code has a subtle bug that often bites people. Both copies (old and new) point to the same instance of ListOfNames & Owner. In the case of the string (Owner) this may not be a problem, as strings are immutable, but for many other things (Lists, Dictionaries, Datasets, etc), it's a catastrophic and subtle bug. If someone goes "old.ListOfNames.Clear()" both instances are affected. Anyone know why Microsoft never made a Generic IClonable? This always seemed silly, as IClonable<T> would eliminate the cast that always happens on clone. I usually end up with two methods: public object Clone(){} public MyType CloneAsMyType() { return (MyType) Clone());} -- Chris Mullins "schneider" <eschneider (AT) ooooooooo (DOT) com> wrote in message news:emvRHGLBIHA.3716 (AT) TK2MSFTNGP03 (DOT) phx.gbl... It seems that in some cases its quicker to clone an existing object vs creating a new object. I have not done exstensive perf test but have found cases where it helps. Anyone have any thoughts on this? Why would this be true? Is it always true? if not then why not? Thanks, Schneider |
#7
| |||
| |||
|
|
Laura's answer is dead on - Cloning & Creating a new Instance are two very different things with very different use cases. In a number of projects I work on, we built the cornerstone of our algorithms on the Prototype Pattern, which is a Clone() based implementation. Cloning is great, and has a wide variety of use cases. In most cases though, cloning is going to be quite a bit slower than creating a new instance. The code for cloning is often going to look like: public object Clone() { Foo o = new Foo(); o.ListOfNames = this.ListOfNames; o.Owner = this.Owner; return o; } This is clearly slower than just creating a new instance of Foo, as more stuff is going on. As Cloning gets more complex (as it always does) it's not uncommon to use Xml or binary Serialization, or even (and this is bad!) do a database dip. Note: The above code has a subtle bug that often bites people. Both copies (old and new) point to the same instance of ListOfNames & Owner. In the case of the string (Owner) this may not be a problem, as strings are immutable, but for many other things (Lists, Dictionaries, Datasets, etc), it's a catastrophic and subtle bug. If someone goes "old.ListOfNames.Clear()" both instances are affected. Anyone know why Microsoft never made a Generic IClonable? This always seemed silly, as IClonable<T> would eliminate the cast that always happens on clone. I usually end up with two methods: public object Clone(){} public MyType CloneAsMyType() { return (MyType) Clone());} -- Chris Mullins "schneider" <eschneider (AT) ooooooooo (DOT) com> wrote in message news:emvRHGLBIHA.3716 (AT) TK2MSFTNGP03 (DOT) phx.gbl... It seems that in some cases its quicker to clone an existing object vs creating a new object. I have not done exstensive perf test but have found cases where it helps. Anyone have any thoughts on this? Why would this be true? Is it always true? if not then why not? Thanks, Schneider |
#8
| |||
| |||
|
|
I guess the problem with generics cloning comes from the possibility that a T> might not be cloneable. Another point is the semantic mismatch of ICloneable interface.. does it mean shallow/deep? You can read a very good discussion here: http://blogs.msdn.com/brada/archive/...03/125427.aspx More interesting cloning subtleties: http://blogs.msdn.com/abhinaba/archi...19/578518.aspx "Chris Mullins [MVP - C#]" <cmullins (AT) yahoo (DOT) com> ha scritto nel messaggio news:OtKtfrRBIHA.3916 (AT) TK2MSFTNGP02 (DOT) phx.gbl... Laura's answer is dead on - Cloning & Creating a new Instance are two very different things with very different use cases. In a number of projects I work on, we built the cornerstone of our algorithms on the Prototype Pattern, which is a Clone() based implementation. Cloning is great, and has a wide variety of use cases. In most cases though, cloning is going to be quite a bit slower than creating a new instance. The code for cloning is often going to look like: public object Clone() { Foo o = new Foo(); o.ListOfNames = this.ListOfNames; o.Owner = this.Owner; return o; } This is clearly slower than just creating a new instance of Foo, as more stuff is going on. As Cloning gets more complex (as it always does) it's not uncommon to use Xml or binary Serialization, or even (and this is bad!) do a database dip. Note: The above code has a subtle bug that often bites people. Both copies (old and new) point to the same instance of ListOfNames & Owner. In the case of the string (Owner) this may not be a problem, as strings are immutable, but for many other things (Lists, Dictionaries, Datasets, etc), it's a catastrophic and subtle bug. If someone goes "old.ListOfNames.Clear()" both instances are affected. Anyone know why Microsoft never made a Generic IClonable? This always seemed silly, as IClonable<T> would eliminate the cast that always happens on clone. I usually end up with two methods: public object Clone(){} public MyType CloneAsMyType() { return (MyType) Clone());} -- Chris Mullins "schneider" <eschneider (AT) ooooooooo (DOT) com> wrote in message news:emvRHGLBIHA.3716 (AT) TK2MSFTNGP03 (DOT) phx.gbl... It seems that in some cases its quicker to clone an existing object vs creating a new object. I have not done exstensive perf test but have found cases where it helps. Anyone have any thoughts on this? Why would this be true? Is it always true? if not then why not? Thanks, Schneider |
#9
| |||
| |||
|
|
It seems that in some cases its quicker to clone an existing object vs creating a new object. I have not done exstensive perf test but have found cases where it helps. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |