0
|
1 using System;
|
|
2 using Jint.Runtime.VM.OpCodes;
|
|
3
|
|
4 namespace Jint.Runtime
|
|
5 {
|
|
6 using VM;
|
|
7 using System.IO;
|
|
8 class MainClass
|
|
9 {
|
|
10 public static void Main(string[] args)
|
|
11 {
|
|
12 var runtime = new RuntimeContext ();
|
|
13
|
|
14 var frame = new Frame (3,runtime);
|
|
15
|
|
16 frame.Set (0, 0);
|
|
17 frame.Set (1, 1);
|
|
18
|
|
19 var op = new Add (0,1,0);
|
|
20
|
|
21 var t = Environment.TickCount;
|
|
22
|
|
23 for(int i=0; i < 10000000; i++)
|
|
24 op.Invoke (frame);
|
|
25
|
|
26 var res = frame.Get<int> (0);
|
|
27
|
|
28 Console.WriteLine ("got: {0}, int {1} ms", res, Environment.TickCount - t );
|
|
29
|
|
30 t = Environment.TickCount;
|
|
31
|
|
32 object count = 0, inc = 1;
|
|
33 for (int i=0; i< 10000000; i++)
|
|
34 count = OpAdd(count,inc);
|
|
35
|
|
36 Console.WriteLine ("reference results: {0}, int {1} ms", count, Environment.TickCount - t );
|
2
|
37
|
|
38 var code = new VM2.Instruction[10000000];
|
|
39
|
|
40 for (int i = 0; i < code.Length; i++)
|
|
41 code[i] = new VM2.Instruction(){ code = VM2.OpCodes.Codes.Add, dest = 0, args = new int[] {0,1} };
|
|
42 var machine = new VM2.Machine ();
|
|
43
|
|
44 t = Environment.TickCount;
|
|
45
|
|
46 machine.InitFrame (new object[] { 0, 1 });
|
|
47 machine.Execute (code);
|
|
48
|
|
49 Console.WriteLine ("vm2: {0}, int {1} ms", machine.Get(0), Environment.TickCount - t );
|
0
|
50 }
|
|
51
|
|
52 public static object OpAdd(object arg1, object arg2) {
|
|
53 if (arg1.GetType () == arg2.GetType ()) {
|
|
54 return OpAddIntegers(arg1,arg2);
|
|
55 }
|
|
56 throw new Exception ();
|
|
57 }
|
|
58
|
|
59 public static object OpAddIntegers(object arg1, object arg2) {
|
|
60 return (int)arg1 + (int)arg2;
|
|
61 }
|
|
62 }
|
|
63 }
|