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

simple loop test
author cin
date Mon, 28 Oct 2013 00:49:15 +0400
parents aced2ae9957f
children
line wrap: on
line diff
--- a/Jint.Runtime/Main.cs	Sun Oct 27 21:20:59 2013 +0400
+++ b/Jint.Runtime/Main.cs	Mon Oct 28 00:49:15 2013 +0400
@@ -12,12 +12,25 @@
         {
             RuntimeContext runtime = new RuntimeContext();
             runtime.DefineBinaryOperation<int>(Codes.Add, (x, y) => x + y);
+            runtime.DefineCompareOperation<int>((x, y) => x.CompareTo(y));
 
-            var frame = new Frame(4,runtime);
+            var frame = new Frame(5,runtime);
+            frame.SetValue(1, 10000000);
             frame.SetValue(2,0);
             frame.SetValue(3,1);
 
-            var op = new BinaryOp(Codes.Add, 2, 3, 2);
+
+            var code = new LteCmp(2, 1, 4);
+            var body = new BinaryOp(Codes.Add, 2, 3, 2);
+            body.Chain(code);
+            code.Chain(
+                new ConditionOp(
+                    4,
+                    body,
+                    null
+                )
+            );
+                        
 
 			var t = Environment.TickCount;
 
@@ -25,38 +38,33 @@
              *          mov r1, 10 000 000
              *          mov r2, 0
              *          mov r3, 1
-             *          loop
-             *              condition:
-             *                  gte r2, r1
-             *              body:
-             *                  add r2,r3,r2
+             *          lte r2, r1, r4
+             * cmp:
+             *          test r4 { success => body, fail => end }
+             * body:
+             *          add r2,r3,r2
+             *          goto cmp
+             * end:
              */
 
-            for (int i = 0; i < 10000000; i++)
-                op.Invoke(frame);
+            for (IInstruction op = code; op != null; op = op.Invoke(frame))
+                ;
 
-			Console.WriteLine ("vm: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t );
+			Console.WriteLine ("vm: {0}, int {1} ms", frame.GetConverted<int>(2), Environment.TickCount - t );
 
             t = Environment.TickCount;
 
-            /*
-             *          mov r1, 10 000 000
-             *          mov r2, 0
-             *          mov r3, 1
-             *          loop
-             *              condition:
-             *                  gte r2, r1
-             *              body:
-             *                  add r2,r3,r2
-             */
-            object count = 0, inc = 1;
+            object count = 0, inc = 1, max = 10000000;
             
-            for (int i = 0; i < 10000000; i++)
+            while(Compare(count,max) <= 0)
                 count = Add(count,inc);
 
             Console.WriteLine("native: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t);
 
-			t = Environment.TickCount;
+        }
+
+        public static int Compare(object arg1, object arg2) {
+            return ((int)arg1).CompareTo((int)arg2);
         }
 
         public static object Add(object arg1, object arg2) {