Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / benchmark / valuetype-hash-equals.cs
1 using System;
2
3 public class ValueType1
4 {
5         static int Main ()
6         {
7                 Blah a = new Blah ("abc", 1);
8                 Blah b = new Blah ("ab" + 'c', 1);
9                 long start, end;
10                 start = Environment.TickCount;
11
12                 start = Environment.TickCount;
13                 for (int i = 0; i < 1000000; i++)
14                         a.GetHashCode ();
15                 end = Environment.TickCount;
16                 Console.WriteLine ("struct common GetHashCode(): {0}", end-start);
17
18                 start = Environment.TickCount;
19                 for (int i = 0; i < 1000000; i++)
20                         a.Equals (b);
21                 end = Environment.TickCount;
22                 Console.WriteLine ("struct common Equals(): {0}", end-start);
23
24                 Blah2 a2 = new Blah2 ("abc", 1);
25                 Blah2 b2 = new Blah2 ("abc", 1);
26                 start = Environment.TickCount;
27                 for (int i = 0; i < 1000000; i++)
28                         a2.GetHashCode ();
29                 end = Environment.TickCount;
30                 Console.WriteLine ("struct specific GetHashCode(): {0}", end-start);
31
32                 start = Environment.TickCount;
33                 for (int i = 0; i < 1000000; i++)
34                         a2.Equals (b2);
35                 end = Environment.TickCount;
36                 Console.WriteLine ("struct specific Equals(): {0}", end-start);
37
38                 return 0;
39         }
40
41         struct Blah
42         { 
43                 public string s;
44                 public int i;
45
46                 public Blah (string s, int k)
47                 {
48                         this.s = s;
49                         i = k;
50                 }
51         }
52
53         struct Blah2
54         { 
55                 public string s;
56                 public int i;
57
58                 public Blah2 (string s, int k)
59                 {
60                         this.s = s;
61                         i = k;
62                 }
63
64                 public override int GetHashCode () {
65                         return i ^ s.GetHashCode ();
66                 }
67                 public override bool Equals (object obj) {
68                         if (obj == null || !(obj is Blah2))
69                                 return false;
70                         Blah2 b = (Blah2)obj;
71                         return b.s == this.s && b.i == this.i;
72                 }
73         }
74 }
75