Skip to content

Instantly share code, notes, and snippets.

@d-chambers
Created April 15, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-chambers/03088018405039a4acd81a1f995a256b to your computer and use it in GitHub Desktop.
Save d-chambers/03088018405039a4acd81a1f995a256b to your computer and use it in GitHub Desktop.
profiling proposed changes in obspy PR 2091
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Profile potential changes made to resoruce_ids\n",
"Profiles the changes made to ResourceIdentifier objects proposed in obspy [PR #2091](https://github.com/obspy/obspy/pull/2091)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import gc\n",
"import io\n",
"import pickle\n",
"\n",
"import obspy\n",
"import obspy.core.event as ev\n",
"from obspy.core.util.testing import create_diverse_catalog\n",
"from obspy import read_events\n",
"from obspy import UTCDateTime, Catalog\n",
"\n",
" \n",
"catalog = create_diverse_catalog()\n",
"\n",
"\n",
"def read_quakeml(*args):\n",
" cat_bytes = io.BytesIO()\n",
" catalog.write(cat_bytes, 'quakeml')\n",
" return obspy.read_events(cat_bytes)\n",
"\n",
"\n",
"def copy_catalog():\n",
" return catalog.copy()\n",
"\n",
"\n",
"def pickle_catalog():\n",
" cat_pickle_bytes = pickle.dumps(catalog)\n",
" return pickle.loads(cat_pickle_bytes)\n",
" \n",
"\n",
"def make_catalog_list(*args):\n",
" \"\"\"\n",
" Make a list of complex catalogs (and copies) and return it.\n",
" \"\"\"\n",
" cat1 = create_diverse_catalog()\n",
" cat2 = read_quakeml()\n",
" cat3 = copy_catalog()\n",
" cat4 = pickle_catalog()\n",
" return [cat1, cat2, cat3, cat4]\n",
"\n",
"\n",
"gc.disable()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Without scope resource_ids\n",
"Swap out `scope_resource_ids` method with a dummy function to simulate not using the method. These serve as a baseline. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"old = ev.Event.scope_resource_ids\n",
"ev.Event.scope_resource_ids = lambda x: x"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13.3 ms ± 130 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit read_quakeml()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.8 ms ± 393 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit copy_catalog()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.71 ms ± 13.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit pickle_catalog()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22.5 ms ± 239 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit make_catalog_list()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Scopped Resource_Ids\n",
"Restore `scope_resource_ids` method in order to profile the performance effecot of the proposed changes."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ev.Event.scope_resource_ids = old"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13.6 ms ± 66.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit read_quakeml()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.07 ms ± 36 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit copy_catalog()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.21 ms ± 20.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit pickle_catalog()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24.6 ms ± 74.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit make_catalog_list()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# uncomment the next two lines to run snakeviz for profiling\n",
"# %load_ext snakeviz\n",
"# %snakeviz copy_catalog()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment