Skip to content

Instantly share code, notes, and snippets.

@pentschev
Last active July 31, 2019 18:43
Show Gist options
  • Save pentschev/cf616610646c9de5ab08e4dc130bd8b3 to your computer and use it in GitHub Desktop.
Save pentschev/cf616610646c9de5ab08e4dc130bd8b3 to your computer and use it in GitHub Desktop.
Cython Malloc Benchmark
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%load_ext cython"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import resource\n",
"pagesize = resource.getpagesize()\n",
"\n",
"mem_size = int(1e9)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.17.0'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.__version__"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"%%cython\n",
"\n",
"from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free\n",
"\n",
"\n",
"cdef class SomeMemory:\n",
"\n",
" cdef unsigned char* data\n",
" cdef size_t nbytes\n",
"\n",
" def __cinit__(self, size_t nbytes):\n",
" self.nbytes = nbytes\n",
" self.data = <unsigned char*> PyMem_Malloc(nbytes)\n",
" if not self.data:\n",
" raise MemoryError()\n",
" \n",
" def set_pages(self, size_t pagesize):\n",
" # We access one byte per page to ensure assignment\n",
" # of actual physical memory pages\n",
" for i in range(self.nbytes // pagesize):\n",
" self.data[i * pagesize] = 1\n",
" \n",
" def set_full(self):\n",
" for i in range(self.nbytes):\n",
" self.data[i] = 1\n",
"\n",
" def __dealloc__(self):\n",
" PyMem_Free(self.data)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"mem = SomeMemory(mem_size)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12.4 ms ± 780 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit mem.set_pages(pagesize)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"mem = SomeMemory(mem_size)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"283 ms ± 1.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit mem.set_full()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"307 ms ± 59.9 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit x = np.ones(mem_size, dtype='u1')"
]
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment