Added some timings
authorNeil Smith <neil.git@njae.me.uk>
Wed, 26 Jul 2017 14:16:25 +0000 (15:16 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Wed, 26 Jul 2017 14:16:25 +0000 (15:16 +0100)
07-interpreter/machine-code.ipynb

index 69cd43ff07b09f91c79e0082aa7cd6c0aa40b2a1..2bb22230c55db64aa3cb6e4069836942bb8a00ee 100644 (file)
     {
      "data": {
       "text/plain": [
-       "{'a': 0,\n",
-       " 1: 10,\n",
-       " 'pc': 6,\n",
-       " 'c': 20,\n",
+       "{1: 10,\n",
+       " 'a': 0,\n",
+       " 'b': 10,\n",
        " 'instructions': [(<function __main__.set_literal>, ['a', 10]),\n",
        "  (<function __main__.dec>, ['a']),\n",
        "  (<function __main__.inc>, ['b']),\n",
        "  (<function __main__.sto>, ['b', 1]),\n",
        "  (<function __main__.jpz>, ['a', 2]),\n",
        "  (<function __main__.jmp>, [-4])],\n",
-       " 'b': 10}"
+       " 'c': 20,\n",
+       " 'pc': 6}"
       ]
      },
      "execution_count": 22,
     {
      "data": {
       "text/plain": [
-       "{'a': 0,\n",
-       " 1: 10,\n",
-       " 'pc': 6,\n",
-       " 'c': 20,\n",
+       "{1: 10,\n",
+       " 'a': 0,\n",
+       " 'b': 10,\n",
        " 'instructions': [(<function __main__.set_literal>, ['a', 10]),\n",
        "  (<function __main__.dec>, ['a']),\n",
        "  (<function __main__.inc>, ['b']),\n",
        "  (<function __main__.sto>, ['b', 1]),\n",
        "  (<function __main__.jpz>, ['a', 2]),\n",
        "  (<function __main__.jmp>, [-4])],\n",
-       " 'b': 10}"
+       " 'c': 20,\n",
+       " 'pc': 6}"
       ]
      },
      "execution_count": 23,
   },
   {
    "cell_type": "code",
-   "execution_count": 39,
+   "execution_count": 28,
    "metadata": {},
    "outputs": [
     {
        "'1: 0, a: 0, b: 0, c: 27, pc: 11'"
       ]
      },
-     "execution_count": 39,
+     "execution_count": 28,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 40,
+   "execution_count": 29,
    "metadata": {},
    "outputs": [
     {
   },
   {
    "cell_type": "code",
-   "execution_count": 29,
+   "execution_count": 30,
    "metadata": {},
    "outputs": [
     {
        "'1: 0, a: 0, b: 0, c: 10, pc: 13'"
       ]
      },
-     "execution_count": 29,
+     "execution_count": 30,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 30,
+   "execution_count": 31,
    "metadata": {},
    "outputs": [
     {
   },
   {
    "cell_type": "code",
-   "execution_count": 31,
+   "execution_count": 32,
    "metadata": {},
    "outputs": [
     {
        "'1: 0, a: 52, b: 0, c: 0, pc: 48'"
       ]
      },
-     "execution_count": 31,
+     "execution_count": 32,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 32,
+   "execution_count": 33,
    "metadata": {},
    "outputs": [
     {
        "40"
       ]
      },
-     "execution_count": 32,
+     "execution_count": 33,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 33,
+   "execution_count": 34,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 34,
+   "execution_count": 35,
    "metadata": {},
    "outputs": [
     {
        "52"
       ]
      },
-     "execution_count": 34,
+     "execution_count": 35,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 35,
+   "execution_count": 36,
    "metadata": {},
    "outputs": [
     {
        "(250504, 937)"
       ]
      },
-     "execution_count": 35,
+     "execution_count": 36,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 36,
+   "execution_count": 41,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(52, 9)"
+      ]
+     },
+     "execution_count": 41,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "max([(max_collatz(i), i) for i in range(1, 10)])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 37,
    "metadata": {
     "scrolled": true
    },
        " (99, 0, 448)]"
       ]
      },
-     "execution_count": 36,
+     "execution_count": 37,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 40,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "1 loop, best of 3: 38.6 s per loop\n"
+     ]
+    }
+   ],
+   "source": [
+    "%%timeit\n",
+    "show_machine(execute(program, initial_state={'a': 937}))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 39,
    "metadata": {
     "collapsed": true
    },