Mercurial > pub > Jint1
comparison Jint.Runtime/Main.cs @ 3:aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
| author | cin |
|---|---|
| date | Sun, 27 Oct 2013 17:23:25 +0400 |
| parents | 4aed85a1f558 |
| children | cb13da6e3349 |
comparison
equal
deleted
inserted
replaced
| 2:4aed85a1f558 | 3:aced2ae9957f |
|---|---|
| 1 using System; | 1 using System; |
| 2 using Jint.Runtime.VM.OpCodes; | 2 using Jint.Runtime.VM; |
| 3 | 3 |
| 4 namespace Jint.Runtime | 4 namespace Jint.Runtime |
| 5 { | 5 { |
| 6 using VM; | 6 using VM; |
| 7 using System.IO; | 7 using System.IO; |
| 8 using Jint.Runtime.VM.OpCodes; | |
| 8 class MainClass | 9 class MainClass |
| 9 { | 10 { |
| 10 public static void Main(string[] args) | 11 public static void Main(string[] args) |
| 11 { | 12 { |
| 12 var runtime = new RuntimeContext (); | 13 RuntimeContext runtime = new RuntimeContext(); |
| 14 runtime.DefineBinaryOperation<int>(Codes.Add, (x, y) => x + y); | |
| 13 | 15 |
| 14 var frame = new Frame (3,runtime); | 16 var frame = new Frame(4,runtime); |
| 17 frame.SetValue(2,0); | |
| 18 frame.SetValue(3,1); | |
| 15 | 19 |
| 16 frame.Set (0, 0); | 20 var op = new BinaryOp(Codes.Add, 2, 3, 2); |
| 17 frame.Set (1, 1); | |
| 18 | |
| 19 var op = new Add (0,1,0); | |
| 20 | 21 |
| 21 var t = Environment.TickCount; | 22 var t = Environment.TickCount; |
| 22 | 23 |
| 23 for(int i=0; i < 10000000; i++) | 24 /* |
| 24 op.Invoke (frame); | 25 * mov r1, 10 000 000 |
| 26 * mov r2, 0 | |
| 27 * mov r3, 1 | |
| 28 * loop | |
| 29 * condition: | |
| 30 * gte r2, r1 | |
| 31 * body: | |
| 32 * add r2,r3,r2 | |
| 33 */ | |
| 25 | 34 |
| 26 var res = frame.Get<int> (0); | 35 for (int i = 0; i < 10000000; i++) |
| 36 op.Invoke(frame); | |
| 27 | 37 |
| 28 Console.WriteLine ("got: {0}, int {1} ms", res, Environment.TickCount - t ); | 38 Console.WriteLine ("vm: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t ); |
| 39 | |
| 40 t = Environment.TickCount; | |
| 41 | |
| 42 /* | |
| 43 * mov r1, 10 000 000 | |
| 44 * mov r2, 0 | |
| 45 * mov r3, 1 | |
| 46 * loop | |
| 47 * condition: | |
| 48 * gte r2, r1 | |
| 49 * body: | |
| 50 * add r2,r3,r2 | |
| 51 */ | |
| 52 object count = 0, inc = 1; | |
| 53 | |
| 54 for (int i = 0; i < 10000000; i++) | |
| 55 count = Add(count,inc); | |
| 56 | |
| 57 Console.WriteLine("native: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t); | |
| 29 | 58 |
| 30 t = Environment.TickCount; | 59 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 ); | |
| 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 ); | |
| 50 } | 60 } |
| 51 | 61 |
| 52 public static object OpAdd(object arg1, object arg2) { | 62 public static object Add(object arg1, object arg2) { |
| 53 if (arg1.GetType () == arg2.GetType ()) { | 63 return (int)arg1 + (int)arg2; |
| 54 return OpAddIntegers(arg1,arg2); | 64 } |
| 55 } | |
| 56 throw new Exception (); | |
| 57 } | |
| 58 | |
| 59 public static object OpAddIntegers(object arg1, object arg2) { | |
| 60 return (int)arg1 + (int)arg2; | |
| 61 } | |
| 62 } | 65 } |
| 63 } | 66 } |
