Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/10/09 17:10:47 (15 years ago)
Author:
epitzer
Message:

cached cloning. (#506)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/NewSerializationTest.cs

    r1321 r1322  
    55using System.IO;
    66
    7 namespace Persistence {
     7namespace Persistence.Test {
    88
    99  public class Root {
    10 
    1110    [Storable]
    1211    public int[] i = new int[] { 3, 4, 5, 6 };
     
    4039    [Storable]
    4140    public string name = "Serial";
     41  }
     42
     43  public class CloneableRoot {
     44    public int[] i = new int[] { 3, 4, 5, 6 };
     45    public string s;
     46    public ArrayList intArray = new ArrayList(new int[] { 1, 2, 3 });
     47    public List<int> intList = new List<int>(new int[] { 321, 312, 321 });
     48    public CloneableCustom c;
     49    public List<CloneableRoot> selfReferences;
     50    public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
     51    public bool boolean = true;
     52    public DateTime dateTime = new DateTime();
     53    public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("test key", 123);
     54    public Dictionary<string, int> dict = new Dictionary<string, int>();   
     55    public object Clone(Dictionary<object, object> twins) {
     56      if (twins.ContainsKey(this))
     57        return twins[this];     
     58      CloneableRoot cr = new CloneableRoot();
     59      twins.Add(this, cr);
     60      cr.i = this.i;
     61      cr.s = this.s;
     62      cr.intArray = new ArrayList(this.intArray);
     63      cr.intList = new List<int>(this.intList);
     64      cr.c = (CloneableCustom)c.Clone(twins);
     65      cr.selfReferences = new List<CloneableRoot>();
     66      for (int i = 0; i < this.selfReferences.Count; i++) {
     67        cr.selfReferences.Add(this);
     68      }
     69      cr.multiDimArray = (double[,])this.multiDimArray.Clone();
     70      cr.dateTime = new DateTime(this.dateTime.Ticks);
     71      cr.kvp = new KeyValuePair<string, int>(this.kvp.Key, this.kvp.Value);
     72      cr.dict = new Dictionary<string, int>(this.dict);
     73      return cr;
     74    }   
     75  }
     76
     77  public class CloneableCustom {   
     78    public int i;   
     79    public CloneableRoot r;   
     80    public string name = "Serial";
     81    public object Clone(Dictionary<object, object> twins) {
     82      if (twins.ContainsKey(this))
     83        return twins[this];
     84      CloneableCustom cc = new CloneableCustom();
     85      twins.Add(this, cc);
     86      cc.i = this.i;
     87      cc.r = (CloneableRoot)this.r.Clone(twins);
     88      cc.name = this.name;
     89      return cc;
     90    }
    4291  }
    4392
     
    52101    [Storable]
    53102    private Nullable<double> dbl = null;
     103  }
     104
     105  public class StorableObject {
     106
     107    [Storable]
     108    Dictionary<int, string> dict;
     109
     110    public void Init() {
     111      this.dict = new Dictionary<int, string>();
     112      for (int i = 0; i < 1000000; i++) {
     113        dict.Add(i, i.ToString());
     114      }
     115    }
     116  }
     117
     118  public class CloneableObject : ICloneable {
     119
     120    Dictionary<int, string> dict;
     121
     122    public void Init() {
     123      this.dict = new Dictionary<int, string>();
     124      for (int i = 0; i < 1000000; i++) {
     125        dict.Add(i, i.ToString());
     126      }
     127    }
     128    public object Clone() {
     129      CloneableObject clone = new CloneableObject();
     130      clone.dict = new Dictionary<int, string>(this.dict);
     131      return clone;
     132    }
    54133  } 
    55134
     
    78157      DeSerializer deSerializer = new DeSerializer();       
    79158      object o = deSerializer.DeSerialize(parser);
    80       Root t = StorableAttribute.Clone(r);
     159      Root t = CloningFactory.DefaultClone(r);
    81160      Console.Out.WriteLine(Util.AutoFormat(o, true));     
    82161    }
     
    96175      DeSerializer deSerializer = new DeSerializer();
    97176      object o = deSerializer.DeSerialize(parser);
    98       Manager n = StorableAttribute.Clone(m);
     177      Manager n = CloningFactory.DefaultClone(m);
    99178      Console.Out.WriteLine(Util.AutoFormat(o, true));     
    100179    }
    101180
     181    public static void SpeedTest() {
     182      StorableObject storable = new StorableObject();
     183      CloneableObject cloneable = new CloneableObject();
     184      Console.Write("initializing...");
     185      storable.Init();
     186      cloneable.Init();
     187      Console.WriteLine("done");
     188      List<StorableObject> storableClones = new List<StorableObject>();
     189      List<CloneableObject> clonableClones = new List<CloneableObject>();
     190      CloningFactory cloningFactory = new CloningFactory();
     191      for (int i = 0; i < 100000; i++) {
     192        Console.Write("cloning storable.. ");
     193        storableClones.Add(cloningFactory.Clone(storable));
     194        Console.WriteLine();
     195        Console.Write("cloning cloneable.. ");
     196        clonableClones.Add((CloneableObject)cloneable.Clone());
     197        Console.WriteLine();
     198      }
     199    }
     200
     201    public static void SpeedTest2() {
     202      Root r = new Root();
     203      r.selfReferences = new List<Root>();
     204      r.selfReferences.Add(r);
     205      r.selfReferences.Add(r);
     206      r.c = new Custom();
     207      r.c.r = r;
     208      r.dict.Add("one", 1);
     209      r.dict.Add("two", 2);
     210      r.dict.Add("three", 3);
     211
     212      CloneableRoot cr = new CloneableRoot();
     213      cr.selfReferences = new List<CloneableRoot>();
     214      cr.selfReferences.Add(cr);
     215      cr.selfReferences.Add(cr);
     216      cr.c = new CloneableCustom();
     217      cr.c.r = cr;
     218      cr.dict.Add("one", 1);
     219      cr.dict.Add("two", 2);
     220      cr.dict.Add("three", 3);     
     221      List<Root> storableClones = new List<Root>();
     222      List<CloneableRoot> clonableClones = new List<CloneableRoot>();     
     223      CloningFactory cloningFactory = new CloningFactory();
     224
     225      DateTime start = DateTime.Now;
     226      Console.Write("cloning storable.. ");
     227      for (int i = 0; i < 100000; i++) {       
     228        storableClones.Add(cloningFactory.Clone(r));                       
     229      }
     230      Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
     231
     232      start = DateTime.Now;
     233      Console.Write("cloning storable again.. ");
     234      for (int i = 0; i < 100000; i++) {
     235        storableClones.Add(cloningFactory.Clone(r));
     236      }
     237      Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
     238
     239
     240      start = DateTime.Now;
     241      Console.Write("cloning cloneable.. ");
     242      for (int i = 0; i < 100000; i++) {       
     243        clonableClones.Add((CloneableRoot)cr.Clone(new Dictionary<object, object>()));
     244      }
     245      Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
     246      clonableClones.Clear();
     247     
     248    }
     249
    102250    public static void Main() {
    103       Test1();
    104       Test2();
     251      //Test1();
     252      //Test2();
     253      //SpeedTest();
     254      SpeedTest2();
    105255      Console.In.ReadLine();
    106256    }
Note: See TracChangeset for help on using the changeset viewer.