Last active
November 19, 2015 05:22
-
-
Save ningsuhen/121838beac0390499edd to your computer and use it in GitHub Desktop.
A simple ipython notebook for benchmarking for loop performance of common programming languages
This file contains 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": [ | |
"# NodeJS vs Ruby vs Python Benchmark\n", | |
"\n", | |
"Runs a for loop for 10000000 times in NodeJS, Ruby and Python" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## NodeJS " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 115, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"26\n" | |
] | |
} | |
], | |
"source": [ | |
"%%script node\n", | |
"var startTime = new Date();\n", | |
"var a = 1;\n", | |
"var b = 2;\n", | |
"for(var i=0; i< 10000000; i++){\n", | |
" var c = a + b;\n", | |
"}\n", | |
"var diff = (new Date().getTime()) - startTime.getTime();\n", | |
"console.log(diff)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Ruby" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 108, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"589.097" | |
] | |
} | |
], | |
"source": [ | |
"%%ruby\n", | |
"start_time = Time.now()\n", | |
"a = 1\n", | |
"b = 2\n", | |
"10000000.times do\n", | |
" c = a + b\n", | |
"end\n", | |
"diff = (Time.now - start_time)\n", | |
"print diff * 1000" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Python" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 114, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"908\n" | |
] | |
} | |
], | |
"source": [ | |
"from datetime import datetime\n", | |
"start_time = datetime.now()\n", | |
"a = 1\n", | |
"b = 2\n", | |
"for i in xrange(0,10000000):\n", | |
" c = a + b\n", | |
"diff = (datetime.now() - start_time)\n", | |
"print (diff.microseconds / 1000)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## GoLang" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Overwriting temp.go\n" | |
] | |
} | |
], | |
"source": [ | |
"%%writefile temp.go\n", | |
"package main\n", | |
"import \"fmt\"\n", | |
"import \"time\"\n", | |
"func main() {\n", | |
"\tstart := time.Now()\n", | |
"\ta := 1\n", | |
"\tb := 2\n", | |
"\tfor i := 0; i < 1000000; i++ {\n", | |
"\t\tc := a + b\n", | |
"\t\t_ = c\n", | |
"\t}\n", | |
"\telapsed := time.Since(start)\n", | |
"\tfmt.Println(elapsed * 1000)\n", | |
"}\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 111, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"323.401ms\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!go run temp.go\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Java" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 122, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Overwriting Temp.java\n" | |
] | |
} | |
], | |
"source": [ | |
"%%writefile Temp.java\n", | |
"class Temp{\n", | |
" public static void main(String[] args){\n", | |
" long startTime = System.currentTimeMillis();\n", | |
" int a = 1;\n", | |
" int b = 2;\n", | |
" for(int i=0; i< 10000000; i++){\n", | |
" int c = a + b;\n", | |
" }\n", | |
" long timeElapsed = System.currentTimeMillis() - startTime;\n", | |
" System.out.println(timeElapsed);\n", | |
" }\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 123, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"3\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!javac Temp.java\n", | |
"!java Temp" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## PHP" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 130, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"354.54201698303" | |
] | |
} | |
], | |
"source": [ | |
"%%script php\n", | |
"<?php\n", | |
"$start_time = microtime(true);\n", | |
"$a = 1;\n", | |
"$b = 2;\n", | |
"for($i=0; $i<10000000; $i++){\n", | |
" $c = $a + $b;\n", | |
"}\n", | |
"$diff = microtime(true) - $start_time;\n", | |
"echo $diff * 1000;" | |
] | |
} | |
], | |
"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.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment