comparison Jint.Runtime/Main.cs @ 5:cb13da6e3349

simple loop test
author cin
date Mon, 28 Oct 2013 00:49:15 +0400
parents aced2ae9957f
children
comparison
equal deleted inserted replaced
4:1ae5b10f7a10 5:cb13da6e3349
10 { 10 {
11 public static void Main(string[] args) 11 public static void Main(string[] args)
12 { 12 {
13 RuntimeContext runtime = new RuntimeContext(); 13 RuntimeContext runtime = new RuntimeContext();
14 runtime.DefineBinaryOperation<int>(Codes.Add, (x, y) => x + y); 14 runtime.DefineBinaryOperation<int>(Codes.Add, (x, y) => x + y);
15 runtime.DefineCompareOperation<int>((x, y) => x.CompareTo(y));
15 16
16 var frame = new Frame(4,runtime); 17 var frame = new Frame(5,runtime);
18 frame.SetValue(1, 10000000);
17 frame.SetValue(2,0); 19 frame.SetValue(2,0);
18 frame.SetValue(3,1); 20 frame.SetValue(3,1);
19 21
20 var op = new BinaryOp(Codes.Add, 2, 3, 2); 22
23 var code = new LteCmp(2, 1, 4);
24 var body = new BinaryOp(Codes.Add, 2, 3, 2);
25 body.Chain(code);
26 code.Chain(
27 new ConditionOp(
28 4,
29 body,
30 null
31 )
32 );
33
21 34
22 var t = Environment.TickCount; 35 var t = Environment.TickCount;
23 36
24 /* 37 /*
25 * mov r1, 10 000 000 38 * mov r1, 10 000 000
26 * mov r2, 0 39 * mov r2, 0
27 * mov r3, 1 40 * mov r3, 1
28 * loop 41 * lte r2, r1, r4
29 * condition: 42 * cmp:
30 * gte r2, r1 43 * test r4 { success => body, fail => end }
31 * body: 44 * body:
32 * add r2,r3,r2 45 * add r2,r3,r2
46 * goto cmp
47 * end:
33 */ 48 */
34 49
35 for (int i = 0; i < 10000000; i++) 50 for (IInstruction op = code; op != null; op = op.Invoke(frame))
36 op.Invoke(frame); 51 ;
37 52
38 Console.WriteLine ("vm: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t ); 53 Console.WriteLine ("vm: {0}, int {1} ms", frame.GetConverted<int>(2), Environment.TickCount - t );
39 54
40 t = Environment.TickCount; 55 t = Environment.TickCount;
41 56
42 /* 57 object count = 0, inc = 1, max = 10000000;
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 58
54 for (int i = 0; i < 10000000; i++) 59 while(Compare(count,max) <= 0)
55 count = Add(count,inc); 60 count = Add(count,inc);
56 61
57 Console.WriteLine("native: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t); 62 Console.WriteLine("native: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t);
58 63
59 t = Environment.TickCount; 64 }
65
66 public static int Compare(object arg1, object arg2) {
67 return ((int)arg1).CompareTo((int)arg2);
60 } 68 }
61 69
62 public static object Add(object arg1, object arg2) { 70 public static object Add(object arg1, object arg2) {
63 return (int)arg1 + (int)arg2; 71 return (int)arg1 + (int)arg2;
64 } 72 }