Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Created August 18, 2015 15:45
Show Gist options
  • Save bbengfort/1b71e319d7941976d714 to your computer and use it in GitHub Desktop.
Save bbengfort/1b71e319d7941976d714 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Serialization Helpers\n",
"\n",
"Testing the performance of serializers in code."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import csv\n",
"import time\n",
"import random\n",
"\n",
"from cStringIO import StringIO"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def random_things(length, minrng=0, maxrng=100):\n",
" for _ in xrange(length):\n",
" yield random.randint(minrng, maxrng)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def commafy_v1(things):\n",
" if not things:\n",
" return \"\"\n",
" \n",
" return \"|\".join(map(str, things))"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def commafy_v2(things):\n",
" if not things:\n",
" return \"\"\n",
" \n",
" data = StringIO()\n",
" writer = csv.writer(data, delimiter=\"|\")\n",
" writer.writerow(list(things))\n",
" return data.getvalue()"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"74,87,99,100,84,51,99,29,64,86,32,60,12,18,83,97,68,96,8,80,2,82,4,91,17,16,58,85,75,79,72,6,81,8,56,94,51,39,47,58,12,71,69,68,45,11,15,74,92,30,65,16,46,68,11,61,73,30,86,82,70,60,97,41,65,79,12,88,34,42,50,41,50,84,48,81,13,77,54,53,70,84,87,65,64,45,86,30,95,27,89,7,80,68,34,31,37,55,14,36,25,25,11,72,35,72,36,85,30,30,37,37,63,67,79,25,4,64,32,47\n",
"\n",
"12|98|97|40|66|72|33|9|45|2|61|7|80|35|66|96|64|29|23|10|14|31|4|66|58|85|17|98|5|58|74|63|71|46|90|27|52|87|1|33|51|69|82|87|63|90|86|33|4|38|37|88|32|59|46|6|78|17|88|38|42|62|43|77|32|19|19|14|60|1|62|22|81|96|10|44|8|94|60|50|43|81|68|89|17|41|58|1|100|48|58|53|88|52|70|39|32|4|84|74|25|86|74|61|23|15|41|2|77|87|85|99|44|55|84|34|77|40|95|39\r\n",
"\n"
]
}
],
"source": [
"print commafy_v1(random_things(120))\n",
"print\n",
"print commafy_v2(random_things(120))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"''\n",
"\n",
"''\n"
]
}
],
"source": [
"print repr(commafy_v1([]))\n",
"print\n",
"print repr(commafy_v2([]))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Timer(object):\n",
" def __init__(self, verbose=False):\n",
" self.verbose = verbose\n",
"\n",
" def __enter__(self):\n",
" self.start = time.time()\n",
" return self\n",
"\n",
" def __exit__(self, *args):\n",
" self.end = time.time()\n",
" self.secs = self.end - self.start\n",
" self.msecs = self.secs * 1000 # millisecs\n",
" if self.verbose:\n",
" print 'elapsed time: %f ms' % self.msecs"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"elapsed time: 2407.552958 ms\n",
"elapsed time: 2254.360914 ms\n"
]
}
],
"source": [
"with Timer(verbose=True) as t:\n",
" for _ in xrange(10000):\n",
" commafy_v1(random_things(120))\n",
"\n",
"with Timer(verbose=True) as t:\n",
" for _ in xrange(10000):\n",
" commafy_v2(random_things(120))"
]
}
],
"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