]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/java/CastTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / performance / java / CastTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.performance.java;
13
14 import org.simantics.db.testing.base.TestCommonPerf;
15
16 /**
17  *
18  */
19 public class CastTest extends TestCommonPerf {
20
21         interface A {
22                 
23                 public int getValue();
24                 
25         }
26         
27         final static class B implements A {
28                 
29                 final private int value;
30                 
31                 public B(int value) {
32                         this.value = value;
33                 }
34                 
35                 @Override
36                 public int getValue() {
37                         return value;
38                 }
39                 
40         }
41
42         final static class C implements A {
43                 
44                 final private int value;
45                 
46                 public C(int value) {
47                         this.value = value;
48                 }
49                 
50                 @Override
51                 public int getValue() {
52                         return value;
53                 }
54                 
55         }
56
57         public static int LEN1 = 10000;
58         public static int LEN2 = 1000000000;
59
60         public void test() throws Exception {
61
62                 A a = new B(1);
63                 B b = new B(1);
64
65                 int counter = 0;
66                 long start = System.nanoTime();
67                 for(int i=0;i<LEN1;i++) counter += a.getValue();  
68                 long duration = System.nanoTime() - start;
69
70                 int counter2 = 0;
71                 long start2 = System.nanoTime();
72                 for(int i=0;i<LEN1;i++) counter2 += ((B)a).value;  
73                 long duration2 = System.nanoTime() - start2;
74
75                 int counter3 = 0;
76                 long start3 = System.nanoTime();
77                 for(int i=0;i<LEN2;i++) counter3 += b.value;  
78                 long duration3 = System.nanoTime() - start3;
79
80                 int counter4 = 0;
81                 long start4 = System.nanoTime();
82                 for(int i=0;i<LEN2;i++) counter4 += compB(a);  
83                 long duration4 = System.nanoTime() - start4;
84                 
85                 System.out.println("Finished in " + 1e-9*duration + "s.");
86                 System.out.println("Finished in " + 1e-9*duration2 + "s.");
87                 System.out.println("Finished in " + 1e-9*duration3 + "s.");
88                 System.out.println("Finished in " + 1e-9*duration4 + "s.");
89                 
90         }
91         
92         private int compA(A a) {
93                 int ret = a.getValue();
94                 return ret + 1;
95         }
96
97         private int compB(A a) {
98                 return ((B)a).value;
99 //              if(a instanceof B) return ((B)a).value;
100 //              else if (a instanceof C) return ((C)a).value;
101 //              else return 0;
102         }
103         
104 }