Last active
December 25, 2015 15:28
-
-
Save jskDr/36c715fed390739fb373 to your computer and use it in GitHub Desktop.
A simple example of PyExecJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Performance Of Codes Using PyExecJS " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "In this note, the performance of embeded Javascript codes using PyExecJS is compared with Python, C and Cython. Moreover, the two variation of javascript codes are tested. Using the 'var' command in javascript, the speed of executing becomes highly faster than the opposite case. Interestingly, the performance of the javascript codes is the fatest than any other implementations tested in this note. " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Calling Javascript using PyExecJS" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 36, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import execjs\n", | |
| "\n", | |
| "myjs = execjs.get()\n", | |
| "js_codes = \"\"\"\n", | |
| "function addup( N) {\n", | |
| " var s = 0.0;\n", | |
| " var ii, jj, kk;\n", | |
| " for( ii = 0; ii < N; ii++) {\n", | |
| " for( jj = 0; jj < N; jj++) {\n", | |
| " for( kk = 0; kk < N; kk++) {\n", | |
| " s += ii * jj * kk + 0.01;\n", | |
| " }\n", | |
| " }\n", | |
| " }\n", | |
| " return s;\n", | |
| "}\n", | |
| "function addup_novar( N) {\n", | |
| " var s = 0.0;\n", | |
| " for( ii = 0; ii < N; ii++) {\n", | |
| " for( jj = 0; jj < N; jj++) {\n", | |
| " for( kk = 0; kk < N; kk++) {\n", | |
| " s += ii * jj * kk + 0.01;\n", | |
| " }\n", | |
| " }\n", | |
| " }\n", | |
| " return s;\n", | |
| "}\n", | |
| "\"\"\"\n", | |
| "compjs = myjs.compile( js_codes)\n", | |
| "\n", | |
| "def addup( N):\n", | |
| " \"\"\"\n", | |
| " Python implementation of addup in javascript\n", | |
| " \"\"\" \n", | |
| " s = 0.0;\n", | |
| " for ii in range( N):\n", | |
| " for jj in range( N):\n", | |
| " for kk in range( N):\n", | |
| " s += ii * jj * kk + 0.01\n", | |
| " return s\n", | |
| "\n", | |
| "# %timeit compjs.call(\"addup\", 200)\n", | |
| "# %timeit compjs.call(\"addup_novar\", 200)\n", | |
| "# %timeit addup( 200)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Using var" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 28, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "10 loops, best of 3: 75.5 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit compjs.call(\"addup\", 100)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 29, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "10 loops, best of 3: 83.3 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit compjs.call(\"addup\", 200)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 30, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 loops, best of 3: 2.69 s per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit compjs.call(\"addup\", 1000)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### No using var" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 31, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 loops, best of 3: 270 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit compjs.call(\"addup_novar\", 100)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 32, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 loops, best of 3: 1.66 s per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit compjs.call(\"addup_novar\", 200)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Comparing with Python" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 34, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "10 loops, best of 3: 136 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit addup( 100)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 35, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 loops, best of 3: 1.08 s per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit addup( 200)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "source": [ | |
| "## Appendix - Comparision with C and Cython" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Direct C implementation" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 37, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import jpyx" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 38, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "100 loops, best of 3: 3.96 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit jpyx.sumup_c(100)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 39, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "10 loops, best of 3: 31.3 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit jpyx.sumup_c(200)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 40, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 loops, best of 3: 3.95 s per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit jpyx.sumup_c(1000)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Cython implementation" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 43, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "%load_ext cython" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 49, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "%%cython\n", | |
| "def addup_pyx( int N):\n", | |
| " \"\"\"\n", | |
| " Python implementation of addup in javascript\n", | |
| " \"\"\" \n", | |
| " cdef float s = 0.0\n", | |
| " cdef int ii, jj, kk\n", | |
| " for ii in range( N):\n", | |
| " for jj in range( N):\n", | |
| " for kk in range( N):\n", | |
| " s += ii * jj * kk + 0.01\n", | |
| " return s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 50, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "100 loops, best of 3: 3.88 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit addup_pyx( 100)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 51, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "10 loops, best of 3: 30.8 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit addup_pyx( 200)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 52, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 loops, best of 3: 3.86 s per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit addup_pyx( 1000)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.6" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This ipython notebook is an example of using PyExecJS in order to embed javascript codes in Python.