473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

MemberWiseClone speed and implementation?

Hello!

I accidentally posted this as a reply to another posting. It is a seperate
posting - sorry :-)

I am programming a cloning service (which is essential to the framework),
and am currently using a custom implementation of the ICloneable interface.
With this in mind, I am wondering if I would be better off just using the
MemberWiseClone method of the inherited base object class.

Having measured the differences in speed, the MemberWiseClone implementation
was is approx. 25% slower than the custom implementation. We were debating
this at work and some suggested that I would benefit from future
improvements in the CLR implementation.

However, as this is a critical part of the framework, I believe a 25%
degradation would make the code suffer too much. Also, I could just run the
tests again when .NET 2.0 is released at a later time and change the cloning
service implementation if necessary.

The MemberWiseClone method forwards the call to an external method. I am
interested in the details of this implementation. Is the CLR using
reflection to clone the objects?

Thanks in advance.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #1
6 13652

"Anders Borum" <a@b.dk> wrote in message
news:OG**************@TK2MSFTNGP14.phx.gbl...
Hello!

I accidentally posted this as a reply to another posting. It is a seperate
posting - sorry :-)
I was wondering why that was posted there, ;).

I am programming a cloning service (which is essential to the framework),
and am currently using a custom implementation of the ICloneable
interface.
With this in mind, I am wondering if I would be better off just using the
MemberWiseClone method of the inherited base object class.

Having measured the differences in speed, the MemberWiseClone
implementation
was is approx. 25% slower than the custom implementation. We were debating
this at work and some suggested that I would benefit from future
improvements in the CLR implementation.

However, as this is a critical part of the framework, I believe a 25%
degradation would make the code suffer too much. Also, I could just run
the
tests again when .NET 2.0 is released at a later time and change the
cloning
service implementation if necessary.

The MemberWiseClone method forwards the call to an external method. I am
interested in the details of this implementation. Is the CLR using
reflection to clone the objects?


I do not believe so, no. If memory serves, MemberwiseClone is effectivly a
memcopy, copying the whole object into a new location and doing whatever bit
twiddling needs to be done to establish a new object. However, by all tests
I've run it looks like custom cloning is even faster on empty objects. It is
a curiosity to say the least.

Some crucial differences to consider:

A manual clone involves a new object creation and thus a constructor
invocation, MemberwiseClone does not invoke a constructor, so if your
constructor does things you want or don't want to happen, you'll need to
make yoru choices based on that.

MemberwiseClone is a bit more version proof. Much like copy constructors, an
IClonable::Clone method is hard to implement virtually, you end up with an
effective hiearchy of virtual Clone(T) implemetnations with every subsequent
class implementing ICloneable::Clone() anew so that the proper class is
created and passed down, any other mechanism will generate a different type.
By using MemberwiseClone you can simply implement the virtual Clone(T)
method and provide a single implementation of IClonable::Clone() which calls
MemberwiseClone and passes the returned object down the Clone hiearchy(or
have it passed up, as you will).

MemberwiseClone is shallow, so for deep copy semantics you'll have to use
some form of custom cloning code.
Nov 16 '05 #2
Hello Daniel
I was wondering why that was posted there, ;).
Yes, I can't even find that posting now and sat waiting for it to appear in
the topics list. It's friday, what can I say .. sat till 02:30 AM in the
morning coding as usual (while the family was sleeping =o)
I do not believe so, no. If memory serves, MemberwiseClone is effectivly a
memcopy, copying the whole object into a new location and doing whatever bit twiddling needs to be done to establish a new object. However, by all tests I've run it looks like custom cloning is even faster on empty objects. It is a curiosity to say the least.
Not sure I put the numbers correct in the original posting, but these are
the results from the performance test. I'm initiating a cloning of some 12,3
mio. objects just to push some numbers.

Initiating cloning of 12800000 objects ..
Calculating custom cloning .. 2,921875 seconds.
Calculating MemberWiseClone .. 4,234375 seconds.

This is the release code with default (release) optimizations from VS .NET
2003 on a Dual 1.2Ghz AthlonMP workstation. The VS .NET console project is
available here:

http://adsl.sphereworks.dk/ClonePerformanceConsole.rar
Some crucial differences to consider:

A manual clone involves a new object creation and thus a constructor
invocation, MemberwiseClone does not invoke a constructor, so if your
constructor does things you want or don't want to happen, you'll need to
make yoru choices based on that.
Didn't know that about MemberWiseClone, but I am not dependend on any
constructor calls (the models are, so to speak, just models and are served
from a set of service factories that sets state).
MemberwiseClone is a bit more version proof. Much like copy constructors, an IClonable::Clone method is hard to implement virtually
Yes, that's true. However, the current architecture contains sealed models
(where the cloning is imlemented currently), so I am in control of the
cloning process. But I do agree that in the case where you'd offer
inheritance from your models, it's a safer path using MemberWiseClone.

My problem is that tests showed MemberWiseClone to be quite slow compared to
the custom implementation. I am, again, not sure if the test I made are 100%
perfect. This is also why I'm asking for your oppinion.
By using MemberwiseClone you can simply implement the virtual Clone(T)
method and provide a single implementation of IClonable::Clone() which calls MemberwiseClone and passes the returned object down the Clone hiearchy(or
have it passed up, as you will).
That's the technique I'm using in the performance test.
MemberwiseClone is shallow, so for deep copy semantics you'll have to use
some form of custom cloning code.


Yes, I'm aware of the differences. However, all my models use valuetypes
internally and those are the values I'm interested in cloning. This is also
the reason for considering MemberWiseClone at all (if a deep copy was
required, I'd probably stick with ICloneable as per .NET recommendations).

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #3

Some crucial differences to consider:

A manual clone involves a new object creation and thus a constructor
invocation, MemberwiseClone does not invoke a constructor, so if your
constructor does things you want or don't want to happen, you'll need to
make yoru choices based on that.


Didn't know that about MemberWiseClone, but I am not dependend on any
constructor calls (the models are, so to speak, just models and are served
from a set of service factories that sets state).
MemberwiseClone is a bit more version proof. Much like copy constructors,

an
IClonable::Clone method is hard to implement virtually


Yes, that's true. However, the current architecture contains sealed models
(where the cloning is imlemented currently), so I am in control of the
cloning process. But I do agree that in the case where you'd offer
inheritance from your models, it's a safer path using MemberWiseClone.

My problem is that tests showed MemberWiseClone to be quite slow compared
to
the custom implementation. I am, again, not sure if the test I made are
100%
perfect. This is also why I'm asking for your oppinion.


Ahh, I'm sorry if I misphrased myself originally, but my tests support
yours, I can't get MemberwiseClone to outperform ICloneable.Clone, although
I'm showing about a considerable difference. Since you make it clear later
on that there is nothing else requiring you to use MemberwiseClone, I would
recommend using ICloneable::Clone until such a point where MemberwiseClone
is required.

FWIW, preliminary tests on the 2.0 framework were no better.

My results for 1 million clone iterations over an emtpy object:

MemberwiseClone Time: 00:00:00.1406250
Clone Time: 00:00:00.0156250
Nov 16 '05 #4
Hello!
Ahh, I'm sorry if I misphrased myself originally, but my tests support
yours, I can't get MemberwiseClone to outperform ICloneable.Clone, although I'm showing about a considerable difference.
The test I've performed indicates that the larger number of fields to be
cloned, the larger difference.
Since you make it clear later
on that there is nothing else requiring you to use MemberwiseClone, I would recommend using ICloneable::Clone until such a point where MemberwiseClone
is required.
I came to the same conclusion, but I thought I'd better be safe than sorry.
FWIW, preliminary tests on the 2.0 framework were no better.

My results for 1 million clone iterations over an emtpy object:

MemberwiseClone Time: 00:00:00.1406250
Clone Time: 00:00:00.0156250


That's a considerable difference. Looking at these numbers, I am quite sure
I won't be using the MemberWiseClone approach. The cloning service is
crucial to the performance of the application so I need to squeze as much
performance from the CLR as possible.

If possible, could I get a copy of your test solution?

Thanks for helping out btw!

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #5

"Anders Borum" <a@b.dk> wrote in message
news:ew**************@TK2MSFTNGP14.phx.gbl...
Hello!
Ahh, I'm sorry if I misphrased myself originally, but my tests support
yours, I can't get MemberwiseClone to outperform ICloneable.Clone,

although
I'm showing about a considerable difference.


The test I've performed indicates that the larger number of fields to be
cloned, the larger difference.
Since you make it clear later
on that there is nothing else requiring you to use MemberwiseClone, I

would
recommend using ICloneable::Clone until such a point where
MemberwiseClone
is required.


I came to the same conclusion, but I thought I'd better be safe than
sorry.
FWIW, preliminary tests on the 2.0 framework were no better.

My results for 1 million clone iterations over an emtpy object:

MemberwiseClone Time: 00:00:00.1406250
Clone Time: 00:00:00.0156250


That's a considerable difference. Looking at these numbers, I am quite
sure
I won't be using the MemberWiseClone approach. The cloning service is
crucial to the performance of the application so I need to squeze as much
performance from the CLR as possible.

If possible, could I get a copy of your test solution?


Sure, this is just a basic "take a shot and see waht happens thing", I
didn't put much effort into it:

using System;

public class Test : ICloneable
{
public Test()
{
//Console.WriteLine("Constructor!"); //uncomment this if you want to test
constructor calls.
}

public object Clone()
{
return new Test();
}
public static void Main(string[] args)
{
Test a = new Test();
a.MemberwiseClone(); //warming the paths a little
a.Clone();

DateTime start, finish;
start = DateTime.Now;
for (int i = 0; i < 1000000; i++)
a.MemberwiseClone();

finish = DateTime.Now;
Console.WriteLine("MemberwiseClone Time: " + (finish - start));
start = DateTime.Now;
for (int i = 0; i < 1000000; i++)
a.Clone();

finish = DateTime.Now;
Console.WriteLine("Clone Time: " + (finish - start));

}
}

It lacks subtlety, but the difference was significant enough I decided not
to go for the slightly more complicated tick count approach or the
considerably more accurat high performance timer approach. I'm lazy that
way, ;).

One interesting possibility is that Clone may well be inlinable(I can't
remember if interface membership excludes inlining off hand), while
MemberwiseClone probably is not. That could be a significant issue across a
huge number of execuations on its own.

My tests were run on a dual 2.2 ghz Xeon, btw.
Nov 16 '05 #6
Hello!
It lacks subtlety, but the difference was significant enough I decided not
to go for the slightly more complicated tick count approach or the
considerably more accurat high performance timer approach. I'm lazy that
way, ;).


Yes, I'm that way too .. the initial pointers told me I didn't need that
kind of precision to make up my mind. I would, however, argue that my
provided solution is more tidy that your example ;-)

Session ended from Nostromo.
Ripley signing out ..

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on...
1
by: lewindha | last post by:
Hey guys I'm trying to populate a drop down list with values from a db table. When I view the page, the drop list is empty. Here is the code: private void populateClients() { string...
1
by: Cedric | last post by:
Hello, I try to create clone of a picturebox throught a inherited class of picturebox where I implemented Icloneable interface. So I use Memberwiseclone to partialy copy members and then...
11
by: Jim Lewis | last post by:
Has anyone found a good link on exactly how to speed up code using pyrex? I found various info but the focus is usually not on code speedup.
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
1
by: =?ISO-8859-15?Q?Ma=EBl_Benjamin_Mettler?= | last post by:
Hello Python-List I hope somebody can help me with this. I spent some time googling for an answer, but due to the nature of the problem lots of unrelevant stuff shows up. Anyway, I...
8
by: mast2as | last post by:
I am sure this topic has been discussed a thousand times and I read a few things about it today on the net. I also want to say I am trying to start a polemic here, I am just curious and willint to...
8
by: pinkfloydfan | last post by:
Hi all I have a class that implements the ICloneable interface whereby the Clone() method returns MemberwiseClone(). One of the elements of the class is an array and although the Clone method...
2
by: bmerlover | last post by:
I am currently using Visual Studio 2008 Pro edition programming a windows form application using Visual C++. I am using datagrids, the user can populate fields with data. I am not using datasets...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.