1: private void testInt(int v1, int v2, int times)
2: { 3: int x = v1, y = v2, z = 0;
4: for (int i = 0; i < times; i++)
5: { 6: z = x + y;
7: z = x - y;
8: z = x * y;
9: z = x / y;
10: }
11: }
12:
13: private void testFloat(float v1, float v2, int times)
14: { 15: float x = v1, y = v2, z = 0;
16: for (int i = 0; i < times; i++)
17: { 18: z = x + y;
19: z = x - y;
20: z = x * y;
21: z = x / y;
22: }
23: }
24:
25: private void testDouble(double v1, double v2, int times)
26: { 27: double x = v1, y = v2, z = 0;
28: for (int i = 0; i < times; i++)
29: { 30: z = x + y;
31: z = x - y;
32: z = x * y;
33: z = x / y;
34: }
35: }
36:
37: private void testDecimal(decimal v1, decimal v2, int times)
38: { 39: decimal x = v1, y = v2, z = 0;
40: for (int i = 0; i < times; i++)
41: { 42: z = x + y;
43: z = x - y;
44: z = x * y;
45: z = x / y;
46: }
47: }
48:
49: private double getTestDuration(int testNo, int times)
50: { 51: DateTime dt = DateTime.Now;
52: switch (testNo)
53: { 54: case 1:
55: testInt(500, 500, times);
56: break;
57: case 2:
58: testFloat(500, 500, times);
59: break;
60: case 3:
61: testDouble(500, 500, times);
62: break;
63: case 4:
64: testDecimal(500, 500, times);
65: break;
66: }
67: TimeSpan ts = DateTime.Now - dt;
68: return ts.TotalMilliseconds;
69: }
70:
71: protected void Page_Load(object sender, EventArgs e)
72: { 73: int TIMES = 10000000;
74: for (int i = 1; i <= 4; i++)
75: { 76: Response.Write(
77: string.Format("<li>Test {0} Duration={1:0.000}ms", 78: i, getTestDuration(i, TIMES)
79: )
80: );
81: }
82:
83: }