Skip to content

Instantly share code, notes, and snippets.

@CodyKochmann
Last active February 19, 2021 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodyKochmann/a52085671e5aa7c736e389901d989ace to your computer and use it in GitHub Desktop.
Save CodyKochmann/a52085671e5aa7c736e389901d989ace to your computer and use it in GitHub Desktop.
lua python generators performance
# This demonstrates the speed difference
# between lua coroutines vs python vs numba
# generators
# by: Cody Kochmann
In [1]: import lupa
In [2]: lua = lupa.LuaRuntime(unpack_returned_tuples=T
...: rue)
In [3]: lua_fib = lua.eval('''
...: function()
...: return coroutine.create(function ()
...: local prev = 0
...: local current = 1
...: local placeholder = current
...: while true do
...: current = current + prev
...: prev = placeholder
...: coroutine.yield(current)
...: placeholder = current
...: end
...: end)
...: end
...: ''')
In [4]: def py_fib():
...: prev = 0
...: current = 1
...: placeholder = current
...: while True:
...: current += prev
...: prev = placeholder
...: yield current
...: placeholder = current
...:
In [5]: import generators
In [6]: generators.performance_tools.runs_per_second(
...: lua_fib().coroutine()
...: )
Out[6]: 3257785
In [7]: generators.performance_tools.runs_per_second(
...: py_fib()
...: )
Out[7]: 156871
In [8]: generators.G(py_fib()).first(50).last()
Out[8]: 20365011074
In [9]: generators.G(lua_fib().coroutine()).first(50).
...: last()
Out[9]: 20365011074
In [10]: import numba
In [11]: numba_fib = numba.jit(py_fib)
In [12]: generators.performance_tools.runs_per_second(
...: numba_fib()
...: )
Out[12]: 7240610
In [13]: generators.G(numba_fib()).first(50).last()
Out[13]: 20365011074
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment