In [1]:
%load_ext cython

In [2]:
%%cython
# cython: boundscheck = True
def f(double[:] x):
    cdef double y
    with nogil:
        y = x[99]
    return y


warning: /Users/siuser/.ipython/cython/_cython_magic_7a8388acbc213c90e3c57d36965066d6.pyx:5:13: Use boundscheck(False) for faster access

In [3]:
import numpy as np

In [4]:
x = np.zeros(3)
print(x)


[ 0.  0.  0.]

In [5]:
f(x)


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-963bfc488912> in <module>()
----> 1 f(x)

_cython_magic_7a8388acbc213c90e3c57d36965066d6.pyx in _cython_magic_7a8388acbc213c90e3c57d36965066d6.f (/Users/siuser/.ipython/cython/_cython_magic_7a8388acbc213c90e3c57d36965066d6.c:1684)()

IndexError: Out of bounds on buffer access (axis 0)

In [6]:
%%cython
# cython: wraparound = True
from libc.stdint cimport uint8_t

def g(uint8_t x):
    cdef uint8_t y = x + 2
    return y

In [7]:
g(255)


Out[7]:
1

In [8]:
%%cython
# cython: wraparound = False

def g(double[:] x):
    return x[-2]


warning: /Users/siuser/.ipython/cython/_cython_magic_d9f6bd8068dc9dbe1667a76cf1d62151.pyx:4:14: the result of using negative indices inside of code sections marked as 'wraparound=False' is undefined

In [9]:
import numpy as np
data = np.random.rand(3)
print(data)
g(data)


[ 0.26966829  0.35610444  0.03265443]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-9-41b25b2f725a> in <module>()
      2 data = np.random.rand(3)
      3 print(data)
----> 4 g(data)

_cython_magic_d9f6bd8068dc9dbe1667a76cf1d62151.pyx in _cython_magic_d9f6bd8068dc9dbe1667a76cf1d62151.g (/Users/siuser/.ipython/cython/_cython_magic_d9f6bd8068dc9dbe1667a76cf1d62151.c:1663)()

IndexError: Out of bounds on buffer access (axis 0)

In [10]:
%%cython -a
# cython: boundscheck = False
cimport cython

@cython.cdivision(False)
def f1(double[:] x, double[:] y):
    cdef int i, j, nx = x.shape[0], ny = y.shape[0]
    cdef double result = 0
    for i in range(nx):
        for j in range(ny):
            result += x[i] / y[j]
    return result

@cython.cdivision(True)
def f2(double[:] x, double[:] y):
    cdef int i, j, nx = x.shape[0], ny = y.shape[0]
    cdef double result = 0
    for i in range(nx):
        for j in range(ny):
            result += x[i] / y[j]
    return result


Out[10]:
Cython: _cython_magic_ae0c425994c14f5954a212728dfe8394.pyx

Generated by Cython 0.25.2

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

+01: # cython: boundscheck = False
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error)
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 02: cimport cython
 03: 
 04: @cython.cdivision(False)
+05: def f1(double[:] x, double[:] y):
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_ae0c425994c14f5954a212728dfe8394_1f1(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_ae0c425994c14f5954a212728dfe8394_1f1 = {"f1", (PyCFunction)__pyx_pw_46_cython_magic_ae0c425994c14f5954a212728dfe8394_1f1, METH_VARARGS|METH_KEYWORDS, 0};
static PyObject *__pyx_pw_46_cython_magic_ae0c425994c14f5954a212728dfe8394_1f1(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  __Pyx_memviewslice __pyx_v_x = { 0, 0, { 0 }, { 0 }, { 0 } };
  __Pyx_memviewslice __pyx_v_y = { 0, 0, { 0 }, { 0 }, { 0 } };
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("f1 (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_x,&__pyx_n_s_y,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_x)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_y)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("f1", 1, 2, 2, 1); __PYX_ERR(0, 5, __pyx_L3_error)
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "f1") < 0)) __PYX_ERR(0, 5, __pyx_L3_error)
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_x = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[0]); if (unlikely(!__pyx_v_x.memview)) __PYX_ERR(0, 5, __pyx_L3_error)
    __pyx_v_y = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[1]); if (unlikely(!__pyx_v_y.memview)) __PYX_ERR(0, 5, __pyx_L3_error)
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("f1", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 5, __pyx_L3_error)
  __pyx_L3_error:;
  __Pyx_AddTraceback("_cython_magic_ae0c425994c14f5954a212728dfe8394.f1", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_46_cython_magic_ae0c425994c14f5954a212728dfe8394_f1(__pyx_self, __pyx_v_x, __pyx_v_y);

  /* function exit code */
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_ae0c425994c14f5954a212728dfe8394_f1(CYTHON_UNUSED PyObject *__pyx_self, __Pyx_memviewslice __pyx_v_x, __Pyx_memviewslice __pyx_v_y) {
  int __pyx_v_i;
  int __pyx_v_j;
  int __pyx_v_nx;
  int __pyx_v_ny;
  double __pyx_v_result;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("f1", 0);
/* … */
  /* function exit code */
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_9);
  __Pyx_AddTraceback("_cython_magic_ae0c425994c14f5954a212728dfe8394.f1", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __PYX_XDEC_MEMVIEW(&__pyx_v_x, 1);
  __PYX_XDEC_MEMVIEW(&__pyx_v_y, 1);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}
/* … */
  __pyx_tuple__14 = PyTuple_Pack(7, __pyx_n_s_x, __pyx_n_s_y, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_nx, __pyx_n_s_ny, __pyx_n_s_result); if (unlikely(!__pyx_tuple__14)) __PYX_ERR(0, 5, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_tuple__14);
  __Pyx_GIVEREF(__pyx_tuple__14);
/* … */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_ae0c425994c14f5954a212728dfe8394_1f1, NULL, __pyx_n_s_cython_magic_ae0c425994c14f5954); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 5, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_f1, __pyx_t_1) < 0) __PYX_ERR(0, 5, __pyx_L1_error)
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_codeobj__15 = (PyObject*)__Pyx_PyCode_New(2, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__14, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_siuser_ipython_cython__cy, __pyx_n_s_f1, 5, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__15)) __PYX_ERR(0, 5, __pyx_L1_error)
+06:     cdef int i, j, nx = x.shape[0], ny = y.shape[0]
  __pyx_v_nx = (__pyx_v_x.shape[0]);
  __pyx_v_ny = (__pyx_v_y.shape[0]);
+07:     cdef double result = 0
  __pyx_v_result = 0.0;
+08:     for i in range(nx):
  __pyx_t_1 = __pyx_v_nx;
  for (__pyx_t_2 = 0; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) {
    __pyx_v_i = __pyx_t_2;
+09:         for j in range(ny):
    __pyx_t_3 = __pyx_v_ny;
    for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {
      __pyx_v_j = __pyx_t_4;
+10:             result += x[i] / y[j]
      __pyx_t_5 = __pyx_v_i;
      if (__pyx_t_5 < 0) __pyx_t_5 += __pyx_v_x.shape[0];
      __pyx_t_6 = (*((double *) ( /* dim=0 */ (__pyx_v_x.data + __pyx_t_5 * __pyx_v_x.strides[0]) )));
      __pyx_t_7 = __pyx_v_j;
      if (__pyx_t_7 < 0) __pyx_t_7 += __pyx_v_y.shape[0];
      __pyx_t_8 = (*((double *) ( /* dim=0 */ (__pyx_v_y.data + __pyx_t_7 * __pyx_v_y.strides[0]) )));
      if (unlikely(__pyx_t_8 == 0)) {
        PyErr_SetString(PyExc_ZeroDivisionError, "float division");
        __PYX_ERR(0, 10, __pyx_L1_error)
      }
      __pyx_v_result = (__pyx_v_result + (__pyx_t_6 / __pyx_t_8));
    }
  }
+11:     return result
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_9 = PyFloat_FromDouble(__pyx_v_result); if (unlikely(!__pyx_t_9)) __PYX_ERR(0, 11, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_9);
  __pyx_r = __pyx_t_9;
  __pyx_t_9 = 0;
  goto __pyx_L0;
 12: 
 13: @cython.cdivision(True)
+14: def f2(double[:] x, double[:] y):
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_ae0c425994c14f5954a212728dfe8394_3f2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_ae0c425994c14f5954a212728dfe8394_3f2 = {"f2", (PyCFunction)__pyx_pw_46_cython_magic_ae0c425994c14f5954a212728dfe8394_3f2, METH_VARARGS|METH_KEYWORDS, 0};
static PyObject *__pyx_pw_46_cython_magic_ae0c425994c14f5954a212728dfe8394_3f2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  __Pyx_memviewslice __pyx_v_x = { 0, 0, { 0 }, { 0 }, { 0 } };
  __Pyx_memviewslice __pyx_v_y = { 0, 0, { 0 }, { 0 }, { 0 } };
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("f2 (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_x,&__pyx_n_s_y,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_x)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_y)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("f2", 1, 2, 2, 1); __PYX_ERR(0, 14, __pyx_L3_error)
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "f2") < 0)) __PYX_ERR(0, 14, __pyx_L3_error)
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_x = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[0]); if (unlikely(!__pyx_v_x.memview)) __PYX_ERR(0, 14, __pyx_L3_error)
    __pyx_v_y = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[1]); if (unlikely(!__pyx_v_y.memview)) __PYX_ERR(0, 14, __pyx_L3_error)
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("f2", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 14, __pyx_L3_error)
  __pyx_L3_error:;
  __Pyx_AddTraceback("_cython_magic_ae0c425994c14f5954a212728dfe8394.f2", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_46_cython_magic_ae0c425994c14f5954a212728dfe8394_2f2(__pyx_self, __pyx_v_x, __pyx_v_y);

  /* function exit code */
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_ae0c425994c14f5954a212728dfe8394_2f2(CYTHON_UNUSED PyObject *__pyx_self, __Pyx_memviewslice __pyx_v_x, __Pyx_memviewslice __pyx_v_y) {
  int __pyx_v_i;
  int __pyx_v_j;
  int __pyx_v_nx;
  int __pyx_v_ny;
  double __pyx_v_result;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("f2", 0);
/* … */
  /* function exit code */
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_7);
  __Pyx_AddTraceback("_cython_magic_ae0c425994c14f5954a212728dfe8394.f2", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __PYX_XDEC_MEMVIEW(&__pyx_v_x, 1);
  __PYX_XDEC_MEMVIEW(&__pyx_v_y, 1);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}
/* … */
  __pyx_tuple__16 = PyTuple_Pack(7, __pyx_n_s_x, __pyx_n_s_y, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_nx, __pyx_n_s_ny, __pyx_n_s_result); if (unlikely(!__pyx_tuple__16)) __PYX_ERR(0, 14, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_tuple__16);
  __Pyx_GIVEREF(__pyx_tuple__16);
/* … */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_ae0c425994c14f5954a212728dfe8394_3f2, NULL, __pyx_n_s_cython_magic_ae0c425994c14f5954); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 14, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_f2, __pyx_t_1) < 0) __PYX_ERR(0, 14, __pyx_L1_error)
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_codeobj__17 = (PyObject*)__Pyx_PyCode_New(2, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__16, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_siuser_ipython_cython__cy, __pyx_n_s_f2, 14, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__17)) __PYX_ERR(0, 14, __pyx_L1_error)
+15:     cdef int i, j, nx = x.shape[0], ny = y.shape[0]
  __pyx_v_nx = (__pyx_v_x.shape[0]);
  __pyx_v_ny = (__pyx_v_y.shape[0]);
+16:     cdef double result = 0
  __pyx_v_result = 0.0;
+17:     for i in range(nx):
  __pyx_t_1 = __pyx_v_nx;
  for (__pyx_t_2 = 0; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) {
    __pyx_v_i = __pyx_t_2;
+18:         for j in range(ny):
    __pyx_t_3 = __pyx_v_ny;
    for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {
      __pyx_v_j = __pyx_t_4;
+19:             result += x[i] / y[j]
      __pyx_t_5 = __pyx_v_i;
      if (__pyx_t_5 < 0) __pyx_t_5 += __pyx_v_x.shape[0];
      __pyx_t_6 = __pyx_v_j;
      if (__pyx_t_6 < 0) __pyx_t_6 += __pyx_v_y.shape[0];
      __pyx_v_result = (__pyx_v_result + ((*((double *) ( /* dim=0 */ (__pyx_v_x.data + __pyx_t_5 * __pyx_v_x.strides[0]) ))) / (*((double *) ( /* dim=0 */ (__pyx_v_y.data + __pyx_t_6 * __pyx_v_y.strides[0]) )))));
    }
  }
+20:     return result
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_7 = PyFloat_FromDouble(__pyx_v_result); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 20, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_7);
  __pyx_r = __pyx_t_7;
  __pyx_t_7 = 0;
  goto __pyx_L0;

In [11]:
def make_data(n):
    x = np.random.rand(n) + 1
    y = np.random.rand(n) + 1
    return x, y

x, y = make_data(10000)

In [12]:
%timeit f1(x, y)
%timeit f2(x, y)


1 loop, best of 3: 419 ms per loop
1 loop, best of 3: 441 ms per loop

In [13]:
-4//3


Out[13]:
-2

In [14]:
416/408


Out[14]:
1.0196078431372548

In [15]:
%%cython -a
# cython: boundscheck = False
# cython: overflowcheck = False
cimport cython

@cython.cdivision(False)
@cython.overflowcheck(False)
def g1(long[:] x, long[:] y):
    cdef int i, j, nx = x.shape[0], ny = y.shape[0]
    cdef long result = 0
    for i in range(nx):
        for j in range(ny):
            result += x[i] / y[j]
    return result

@cython.cdivision(True)
def g2(long[:] x, long[:] y):
    cdef int i, j, nx = x.shape[0], ny = y.shape[0]
    cdef long result = 0
    for i in range(nx):
        for j in range(ny):
            result += x[i] / y[j]
    return result


Error compiling Cython file:
------------------------------------------------------------
...
def g1(long[:] x, long[:] y):
    cdef int i, j, nx = x.shape[0], ny = y.shape[0]
    cdef long result = 0
    for i in range(nx):
        for j in range(ny):
            result += x[i] / y[j]
                  ^
------------------------------------------------------------

/Users/siuser/.ipython/cython/_cython_magic_4efb6f3aa13c0a8a5a8b487bf0f6f1cb.pyx:12:19: Cannot assign type 'double' to 'long'

In [16]:
import numpy as np
def make_data(n):
    x = np.random.random_integers(1, 10, n)
    y = np.random.random_integers(1, 10, n)
    return x, y

a, b = make_data(10000)


/Users/siuser/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:3: DeprecationWarning: This function is deprecated. Please call randint(1, 10 + 1) instead
  app.launch_new_instance()
/Users/siuser/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:4: DeprecationWarning: This function is deprecated. Please call randint(1, 10 + 1) instead

In [17]:
%timeit g1(a, b)
%timeit g2(a, b)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-b067cb9c1913> in <module>()
----> 1 get_ipython().magic('timeit g1(a, b)')
      2 get_ipython().magic('timeit g2(a, b)')

/Users/siuser/anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2156         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2157         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158         return self.run_line_magic(magic_name, magic_arg_s)
   2159 
   2160     #-------------------------------------------------------------------------

/Users/siuser/anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081 

<decorator-gen-58> in timeit(self, line, cell)

/Users/siuser/anaconda/lib/python3.5/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

/Users/siuser/anaconda/lib/python3.5/site-packages/IPython/core/magics/execution.py in timeit(self, line, cell)
   1042             number = 1
   1043             for _ in range(1, 10):
-> 1044                 time_number = timer.timeit(number)
   1045                 worst_tuning = max(worst_tuning, time_number / number)
   1046                 if time_number >= 0.2:

/Users/siuser/anaconda/lib/python3.5/site-packages/IPython/core/magics/execution.py in timeit(self, number)
    137         gc.disable()
    138         try:
--> 139             timing = self.inner(it, self.timer)
    140         finally:
    141             if gcold:

<magic-timeit> in inner(_it, _timer)

NameError: name 'g1' is not defined

In [ ]: