AlanCristhian / statically

Compiles a python function with cython using only a decorator.
MIT License
185 stars 6 forks source link

No speed up in function comparison #8

Closed nayefc closed 6 years ago

nayefc commented 6 years ago

Hello,

Any idea why both of these run in 15.5us?

import cython
import statically
from IPython import get_ipython
ipython = get_ipython()

W = 100
X = 999
Y = 103.13

def calcpy(a, b, c):
    val = 0
    for i in range(100):
        val += a + b * c + i
    return val

@statically.typed
def calcc(a: cython.int, b: cython.int, c: cython.float):
    val: cython.float
    i: cython.int
    for i in range(100):
        val += a + b * c + i
    return val

print('py:')
ipython.magic("timeit calcpy(W, X, Y)")
print('c in py:')
ipython.magic("timeit calcpy(W, X, Y)")

Are my val and i variables not getting cythonized properly?

jared-mackey commented 6 years ago

@nayefc seems you're running the non-cythonized version twice.

In [2]: import cython
   ...: import statically
   ...: from IPython import get_ipython
   ...: ipython = get_ipython()
   ...:
   ...: W = 100
   ...: X = 999
   ...: Y = 103.13
   ...:
   ...: def calcpy(a, b, c):
   ...:     val = 0
   ...:     for i in range(100):
   ...:         val += a + b * c + i
   ...:     return val
   ...:
   ...: @statically.typed
   ...: def calcc(a: cython.int, b: cython.int, c: cython.float):
   ...:     val: cython.float
   ...:     i: cython.int
   ...:     for i in range(100):
   ...:         val += a + b * c + i
   ...:     return val
   ...:
   ...: print('py:')
   ...: ipython.magic("timeit calcpy(W, X, Y)")
   ...: print('c in py:')
   ...: ipython.magic("timeit calcc(W, X, Y)")
   ...:
   ...:
py:
13.9 µs ± 238 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
c in py:
251 ns ± 3.88 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
nayefc commented 6 years ago

Oops... thanks!