Created
February 24, 2017 18:25
-
-
Save arturom/a4f7eabd800e150edd289ed0b360a1ce 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": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"=> \"\u001b[32mSL.UserSignedUp {\"userID\":10}\u001b[39m\"" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"$encoded = 'SL.UserSignedUp {\"userID\":10}';" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"=> \u001b[36mnull\u001b[39m" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"class Timer\n", | |
"{\n", | |
" private $startTime;\n", | |
" \n", | |
" public function __construct()\n", | |
" {\n", | |
" $this->reset();\n", | |
" }\n", | |
" public function reset()\n", | |
" {\n", | |
" $this->startTime = microtime(true);\n", | |
" }\n", | |
" public function checkpoint()\n", | |
" {\n", | |
" return microtime(true) - $this->startTime;\n", | |
" }\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"13.794004201889\n" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"=> \u001b[36mnull\u001b[39m" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"$iterations = 10000000;\n", | |
"$encoded = 'SL.UserSignedUp {\"userID\":10}';\n", | |
"$timer = new Timer();\n", | |
"for ($i = 0; $i < $iterations; $i++) {\n", | |
" sscanf($encoded, '%s');\n", | |
"}\n", | |
"echo $timer->checkpoint();" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"70.204221963882\n" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"=> \u001b[36mnull\u001b[39m" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"$iterations = 10000000;\n", | |
"$encoded = 'SL.UserSignedUp {\"userID\":10}';\n", | |
"$timer = new Timer();\n", | |
"for ($i = 0; $i < $iterations; $i++) {\n", | |
" rtrim(substr($encoded, 0, 32));\n", | |
"}\n", | |
"echo $timer->checkpoint();" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\u001b[31;1mPHP Fatal error: Cannot redeclare strLenStr() in eval()'d code on line 4\u001b[39;22m" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function strLenStr($str)\n", | |
"{\n", | |
" return strlen($str);\n", | |
"}\n", | |
"\n", | |
"function strLenArr(array $str)\n", | |
"{\n", | |
" return strlen($str[0]);\n", | |
"}\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"18.047049045563\n" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"22.029206991196\n" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"21.150142908096\n" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"=> \u001b[36mnull\u001b[39m" | |
] | |
}, | |
{ | |
"data": {}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"$iterations = 10000000;\n", | |
"$encoded = 'SL.UserSignedUp {\"userID\":10}';\n", | |
"\n", | |
"$timer = new Timer();\n", | |
"for ($i = 0; $i < $iterations; $i++) {\n", | |
" strLenStr($encoded);\n", | |
"}\n", | |
"echo $timer->checkpoint();\n", | |
"\n", | |
"$timer = new Timer();\n", | |
"$cached = [$encoded];\n", | |
"for ($i = 0; $i < $iterations; $i++) {\n", | |
" strLenArr([$encoded]);\n", | |
"}\n", | |
"echo $timer->checkpoint();\n", | |
"\n", | |
"$timer = new Timer();\n", | |
"$cached = [$encoded];\n", | |
"for ($i = 0; $i < $iterations; $i++) {\n", | |
" strLenArr($cached);\n", | |
"}\n", | |
"echo $timer->checkpoint();" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "PHP", | |
"language": "php", | |
"name": "jupyter-php" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment