Commit 9a345782 authored by paugier's avatar paugier

Add bench_cort.ipynb

parent 74b7f8b8
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"from transonic import jit, wait_for_all_extensions\n",
"from numba import njit\n",
"\n",
"def cort(s1, s2):\n",
" \"\"\" Computes the cort between series one and two (assuming they have the same length)\n",
"\n",
" :param s1: the first series (or any iterable over floats64)\n",
" :param s2: the second series (or any iterable over floats64)\n",
" :returns: the cort distance\n",
" :rtype: float64\n",
"\n",
" \"\"\"\n",
" slope_1 = s1[1:] - s1[:-1]\n",
" slope_2 = s2[1:] - s2[:-1]\n",
" num = np.sum(slope_1 * slope_2)\n",
" sum_square_x = np.sum(slope_1 * slope_1)\n",
" sum_square_y = np.sum(slope_2 * slope_2)\n",
" return num / (np.sqrt(sum_square_x * sum_square_y))\n",
"\n",
"\n",
"def cort_loop(s1, s2):\n",
" \"\"\" Computes the cort between serie one and two (assuming they have the same length)\n",
"\n",
" :param s1: the first serie (or any iterable over floats64)\n",
" :param s2: the second serie (or any iterable over floats64)\n",
" :returns: the cort distance\n",
" :rtype: float64\n",
"\n",
" \"\"\"\n",
" num = 0.0\n",
" sum_square_x = 0.0\n",
" sum_square_y = 0.0\n",
" for t in range(len(s1) - 1):\n",
" slope_1 = s1[t + 1] - s1[t]\n",
" slope_2 = s2[t + 1] - s2[t]\n",
" num += slope_1 * slope_2\n",
" sum_square_x += slope_1 * slope_1\n",
" sum_square_y += slope_2 * slope_2\n",
" return num / (np.sqrt(sum_square_x * sum_square_y))\n",
"\n",
"kwargs = dict(native=True, xsimd=True)\n",
"cort_trans = jit(cort, **kwargs)\n",
"cort_loop_trans = jit(cort_loop, **kwargs)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"cort_numba = njit(cort)\n",
"cort_loop_numba = njit(cort_loop)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32mINFO \u001b[0m \u001b[34mSchedule pythranization of file /home/users/augier3pi/.transonic/__jit__/__ipython__609910cf9978c2d44e7d73758a7f93e0/cort.py\u001b[0m\n",
"\u001b[32mINFO \u001b[0m \u001b[34mSchedule pythranization of file /home/users/augier3pi/.transonic/__jit__/__ipython__609910cf9978c2d44e7d73758a7f93e0/cort_loop.py\u001b[0m\n"
]
}
],
"source": [
"functions = [cort, cort_loop, cort_trans, cort_loop_trans, cort_numba, cort_loop_numba]\n",
"\n",
"s1 = np.random.rand(10000)\n",
"s2 = s1 + 1\n",
"\n",
"for func in functions[2:]:\n",
" func(s1, s2)\n",
"\n",
"wait_for_all_extensions()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Numpy\n",
"cort\n",
"71.2 µs ± 565 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"cort_loop\n",
"9.33 ms ± 100 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"print(\"Numpy\")\n",
"for func in functions[:2]:\n",
" print(func.__name__)\n",
" %timeit func(s1, s2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pythran\n",
"cort\n",
"21 µs ± 358 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"cort_loop\n",
"11.6 µs ± 14.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"print(\"Pythran\")\n",
"for func in functions[2:4]:\n",
" print(func.__name__)\n",
" %timeit func(s1, s2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Numba\n",
"cort\n",
"65.4 µs ± 1.57 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"cort_loop\n",
"18.7 µs ± 4.55 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"print(\"Numba\")\n",
"for func in functions[4:]:\n",
" print(func.__name__)\n",
" %timeit func(s1, s2)"
]
}
],
"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": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment