In [5]:
from subprocess import check_call, check_output
import os
# import sh

In [13]:
ls = check_output(['ls', '-l', '/'])
print(ls.decode())
print(os.listdir('/'))
mdfind = check_output(['mdfind', '-name', 'test.py'])


total 21
drwxrwxr-x+ 156 root    admin  4992 Dec 18 20:34 Applications
drwxrwxrwt    4 root    wheel   128 Aug 15  2015 DamagedFiles
drwxr-xr-x    2 root    wheel    64 Aug 30  2017 Downloads
drwxrwxr-x    9 root    admin   288 Dec  1  2014 Incompatible Software
drwxr-xr-x+  69 root    wheel  2208 Sep 26 11:26 Library
drwxr-xr-x    2 root    wheel    64 Sep 26 11:18 Network
drwxr-xr-x@   5 root    wheel   160 Sep 21 07:05 System
-rw-r--r--@   1 root    wheel     4 Jun 14  2018 TEST.txt
lrwxr-xr-x    1 root    wheel    49 Oct  3  2014 User Information -> /Library/Documentation/User Information.localized
drwxr-xr-x    6 root    admin   192 Sep 26 11:18 Users
drwxr-xr-x@   6 root    wheel   192 Dec 19 08:50 Volumes
drwxr-xr-x@  37 root    wheel  1184 Dec 13 10:21 bin
drwxrwxr-t    2 root    admin    64 Sep 26 11:18 cores
dr-xr-xr-x    3 root    wheel  4374 Dec 13 10:35 dev
lrwxr-xr-x@   1 root    wheel    11 Sep 26 11:16 etc -> private/etc
dr-xr-xr-x    2 root    wheel     1 Dec 17 19:07 home
-rw-r--r--    1 root    wheel   313 Aug 18 03:55 installer.failurerequests
drwxr-xr-x    3 hombit  staff    96 Mar  7  2015 mnt
dr-xr-xr-x    2 root    wheel     1 Dec 17 19:07 net
drwxr-xr-x@   5 root    wheel   160 Nov 28 11:50 opt
drwxr-xr-x    6 root    wheel   192 Sep 26 11:18 private
drwxr-xr-x@  64 root    wheel  2048 Dec 13 10:21 sbin
lrwxr-xr-x@   1 root    wheel    11 Sep 26 11:16 tmp -> private/tmp
drwxr-xr-x@  11 root    wheel   352 Oct 31 21:04 usr
lrwxr-xr-x@   1 root    wheel    11 Sep 26 11:16 var -> private/var

['.HFS+ Private Directory Data\r', 'home', 'usr', '.Spotlight-V100', 'net', '.DS_Store', '.PKInstallSandboxManager', '.PKInstallSandboxManager-SystemSoftware', 'bin', 'DamagedFiles', 'Incompatible Software', 'installer.failurerequests', 'Network', 'sbin', '.file', 'etc', 'User Information', 'var', 'Library', '.Trashes', 'System', '.OSInstallerMessages', 'mnt', '.fseventsd', 'private', '.DocumentRevisions-V100', '.vol', 'Users', 'Applications', 'opt', 'dev', 'TEST.txt', 'Volumes', 'Downloads', 'tmp', '.dbfseventsd', 'cores']

In [81]:
import ctypes
from ctypes.util import find_library
import numpy
from numpy.testing import assert_allclose
import numba

In [57]:
print(find_library('gsl'))
libm = ctypes.CDLL(find_library('m'))
x = 1
csin = libm.sin
csin.restype = ctypes.c_double
assert_allclose(csin(ctypes.c_double(x)), np.sin(x))
csin.argtypes = (ctypes.c_double,)
assert_allclose(csin(x), np.sin(x))
print(csin(x))

erf = libm.erf
erf.argtypes = (ctypes.c_double,)
erf.restype = ctypes.c_double
erf(x)


/usr/local/lib/libgsl.dylib
0.8414709848078965
Out[57]:
0.8427007929497148

In [69]:
libc = ctypes.CDLL(find_library('c'))
from time import time, ctime
ctime(libc.time(None))


Out[69]:
'Wed Dec 19 09:44:49 2018'

In [70]:
%load_ext cython

In [90]:
%%cython --annotate

def golden_ratio_cython(int n):
    cdef double s = 1.
    cdef int i
    for i in range(n):
        s = 1. + 1. / s
    return s


Out[90]:
Cython: _cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe.pyx

Generated by Cython 0.28.3

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

 1: 
+2: def golden_ratio_cython(int n):
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe_1golden_ratio_cython(PyObject *__pyx_self, PyObject *__pyx_arg_n); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe_1golden_ratio_cython = {"golden_ratio_cython", (PyCFunction)__pyx_pw_46_cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe_1golden_ratio_cython, METH_O, 0};
static PyObject *__pyx_pw_46_cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe_1golden_ratio_cython(PyObject *__pyx_self, PyObject *__pyx_arg_n) {
  int __pyx_v_n;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("golden_ratio_cython (wrapper)", 0);
  assert(__pyx_arg_n); {
    __pyx_v_n = __Pyx_PyInt_As_int(__pyx_arg_n); if (unlikely((__pyx_v_n == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 2, __pyx_L3_error)
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L3_error:;
  __Pyx_AddTraceback("_cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe.golden_ratio_cython", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_46_cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe_golden_ratio_cython(__pyx_self, ((int)__pyx_v_n));

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

static PyObject *__pyx_pf_46_cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe_golden_ratio_cython(CYTHON_UNUSED PyObject *__pyx_self, int __pyx_v_n) {
  double __pyx_v_s;
  CYTHON_UNUSED int __pyx_v_i;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("golden_ratio_cython", 0);
/* … */
  /* function exit code */
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_AddTraceback("_cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe.golden_ratio_cython", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}
/* … */
  __pyx_tuple_ = PyTuple_Pack(4, __pyx_n_s_n, __pyx_n_s_n, __pyx_n_s_s, __pyx_n_s_i); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 2, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_tuple_);
  __Pyx_GIVEREF(__pyx_tuple_);
/* … */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_5c7f53f0dbfbe01b7c1ebd4577e829fe_1golden_ratio_cython, NULL, __pyx_n_s_cython_magic_5c7f53f0dbfbe01b7c); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 2, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_golden_ratio_cython, __pyx_t_1) < 0) __PYX_ERR(0, 2, __pyx_L1_error)
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+3:     cdef double s = 1.
  __pyx_v_s = 1.;
 4:     cdef int i
+5:     for i in range(n):
  __pyx_t_1 = __pyx_v_n;
  __pyx_t_2 = __pyx_t_1;
  for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {
    __pyx_v_i = __pyx_t_3;
+6:         s = 1. + 1. / s
    if (unlikely(__pyx_v_s == 0)) {
      PyErr_SetString(PyExc_ZeroDivisionError, "float division");
      __PYX_ERR(0, 6, __pyx_L1_error)
    }
    __pyx_v_s = (1. + (1. / __pyx_v_s));
  }
+7:     return s
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_4 = PyFloat_FromDouble(__pyx_v_s); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 7, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_r = __pyx_t_4;
  __pyx_t_4 = 0;
  goto __pyx_L0;

In [91]:
def golden_ratio_naive(n):
    s = 1
    for _ in range(n):
        s = 1 + 1 / s
    return s

def golden_ratio_float(n):
    s = 1.
    for _ in range(n):
        s = 1. + 1. / s
    return s

@numba.jit
def golden_ratio_numba(n):
    s = 1.
    for _ in range(n):
        s = 1. + 1. / s
    return s

%timeit golden_ratio_cython(100_000)
%timeit golden_ratio_naive(100_000)
%timeit golden_ratio_float(100_000)
%timeit golden_ratio_numba(100_000)


797 µs ± 51.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
11.9 ms ± 288 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
8.68 ms ± 1.03 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
783 µs ± 8.34 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [108]:
%%cython

cimport cython
from libc.math cimport erf
cimport numpy as cnp
import numpy as np

# def erf_cy(x):
#     return erf(x)

@cython.boundscheck(False)
@cython.wraparound(False)
def erf_cy(cnp.ndarray[double, ndim=1] a):
    cdef cnp.ndarray[double, ndim=1] b = np.empty_like(a)
    cdef double[:] a_view = a
    cdef double[:] b_view = b
    cdef int i
    cdef int size = a.size
    with nogil:
        for i in range(size):
            b_view[i] = erf(a_view[i])
    return b

In [109]:
from scipy import special

a = np.ones(10**5)
%timeit erf_cy(a)
%timeit special.erf(a)


2.93 ms ± 30.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
772 µs ± 67.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [ ]: