{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Additional comparion between java and pypy only, basic sum in a loop with bignits, no Miller Rabin.\n", "Python code first:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting bigint_java_vs_pypy.py\n" ] } ], "source": [ "%%writefile bigint_java_vs_pypy.py\n", "\n", "def sum_of_prime_forth_powers(n):\n", " sum = 0\n", " i = 2\n", " while i <= n:\n", " if True:\n", " sum += i *i * i * i\n", " i += 1\n", " else:\n", " i += 1\n", " return sum\n", "print(sum_of_prime_forth_powers(1000000))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Java code:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "import java.lang.Math.*;\r\n", "import java.math.BigInteger;\r\n", "import java.util.*;\r\n", "\r\n", "\t\t\r\n", "\tpublic class Bigint_sum {\r\n", "\r\n", "\t\tpublic static BigInteger sum_of_forth_power_primes(long n) {\r\n", "\t\tlong i = 2;\r\n", "\t\tBigInteger SUM = BigInteger.ZERO;\r\n", "\t\t\r\n", "\t\twhile (i <= n) {\r\n", "\t\t\tBigInteger tmp = BigInteger.valueOf(i);\r\n", "\t\t\tif (true) {\r\n", "\t\t\t\tSUM = SUM.add(tmp.multiply(tmp.multiply(tmp.multiply(tmp))));\r\n", "\t\t\t\ti += 1;\r\n", "\t\t\t}\r\n", "\t\t\telse\r\n", "\t\t\t\ti += 1;\r\n", "\t\t}\r\n", "\t\treturn SUM;\r\n", "\t}\t\r\n", "\r\n", "\tpublic static void main(String [] args) {\r\n", "\t\t\r\n", "\t\t\r\n", "\t\tSystem.out.println(sum_of_forth_power_primes(1000000).toString());\r\n", "\t\t\r\n", "\t}\r\n", "}\r\n", "\r\n" ] } ], "source": [ "! cat Bigint_sum.java" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "! javac Bigint_sum.java" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tests:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200000500000333333333333299999\n", "200000500000333333333333299999\n", "200000500000333333333333299999\n", "200000500000333333333333299999\n", "1 loop, best of 3: 347 ms per loop\n" ] } ], "source": [ "%timeit ! java Bigint_sum" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200000500000333333333333299999\n", "200000500000333333333333299999\n", "200000500000333333333333299999\n", "200000500000333333333333299999\n", "1 loop, best of 3: 252 ms per loop\n" ] } ], "source": [ "%timeit ! pypy bigint_java_vs_pypy.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Results:\n", "1. python -> 252 ms\n", "2. java -> 347 ms\n", "While java is faster with longs, pypy wins when bigints comes into the play." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }