Last active
April 9, 2017 15:47
-
-
Save eteresh/03bda25e51047a5903c892111066a749 to your computer and use it in GitHub Desktop.
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": [ | |
| "## Compare speed of cpp-program with pure python" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from __future__ import print_function, division" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def main():\n", | |
| " z = 0.0\n", | |
| " for idx in xrange(10**8):\n", | |
| " z += idx / (idx * idx * idx + 700.0)\n", | |
| " print('z =', z)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "z = 0.136066788376\n", | |
| "CPU times: user 49.4 s, sys: 129 ms, total: 49.5 s\n", | |
| "Wall time: 49.4 s\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%time\n", | |
| "main()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### python spend ~ 50 seconds for the task" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Overwriting calc.cpp\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%file calc.cpp\n", | |
| "#include <iostream>\n", | |
| "#include <cstddef>\n", | |
| "\n", | |
| "int main() {\n", | |
| " double z = 0.0;\n", | |
| " double idx_d;\n", | |
| " for (std::size_t idx = 0; idx < 100000000; ++idx) {\n", | |
| " idx_d = idx;\n", | |
| " z += idx_d / (idx_d * idx_d * idx_d + 700.0);\n", | |
| " } \n", | |
| " std::cout << \"z = \" << z << std::endl; \n", | |
| "}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "!g++ -std=c++11 calc.cpp -o calc" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "z = 0.136067\n", | |
| "CPU times: user 25 ms, sys: 6 ms, total: 31 ms\n", | |
| "Wall time: 849 ms\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%time\n", | |
| "!./calc" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### cpp-program spend ~ 0.8 second for the task" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## cpp-program is almost 100 times faster than python on that simple example" | |
| ] | |
| } | |
| ], | |
| "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.13" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment