Package: cython-npm
Cython project management like npm in nodejs. This project is inspired
by npm in nodejs.InstallationYou can easily install by:pip install cython-npmWhat problems does it solve ?When using cython, we face the problem of compile cython file. We can do
it easily by:importpyximport;pyximport.install()But that it is not recommended to letpyximportbuild code on end
user side as ithooks into their import system. The best way to cater
for end users is to provide pre-built binary packages. So this project
compiles .pyx file and provides pre-built binary packages for easy of
use.Quickstart:Basic use to Complie file or folderfromcython_npm.cythoncompileimportexportexport('examplefile.pyx')export('./examplefolder')# then import them to useimportexamplefilefromexamplefolderimport*You should do this code once time only.Create install file like package.jsonYou can also compile many files or folders at once time. Create a file
nameinstall.pyin the root of your project/package and write the
code below:fromcython_npm.cythoncompileimportinstallManymodules=[# put your modules list here'examplefile.pyx','./examplefolder']install(Manymodules)Run the file before start your projectpython install.pyOr add first lineimport installin startup file of your project.
Use install or export in parent folder will compile all .pyx file in
subdirectories. ### Using require(‘path’) as nodejs You can also
relative or fullpath import in python byrequirefunction. For
example:fromcython_npm.cythoncompileimportrequire# import .pyx file. Will cause error if it is not compiled by export() yet.# Default value of recompile is True, only apply for .py file. To import .pyx, change recompile=Falseexamplefile=require('../parentpackage',recompile=False)# import cython package from parent folderexamplefile.somefunction()# it also support relative import .py fileexamplefile=require('../parentpackage')examplefile.somefunction()Using requirepyx(‘path’):requirepyxis simillar torequireexcept: * Use for cython file (‘.pyx’) only * Equivalent to
export(‘.pyx file’) and require(‘.pyx file’) Example:fromcython_npm.cythoncompileimportexportexport('examplefile')require('examplefile',recompile=False)# The code above is the same as:fromcython_npm.cythoncompileimportrequirepyxrequirepyx('examplefile')Using typecheckAnother utils is typecheck support to raise error in typing module (from
python 3.3):fromcython_npm.typecheckimporttypecheck@type_checkdefcheckstr(s:Any)->(None,str):returnNone,sx,y=checkstr('tuan')print(x,y)try:checkstr(120)exceptExceptionaserror:print(error)traceback.print_exc()# That will raise an error of TypeErrorcheckstr(200)Example: Cython vs speed test battleThis example compare the speed between cython vs python, Swift, Go and
Code differences in doing a short calculation. Cython_npm is used in
the test. This test is forked from ‘marcinkliks’, the original code and
test is here:Swift vs Go vs Python
battle. Note:
We use Swift and Go test results as pattern and do not retest them. Go
to see in test folder in github for more examplesTesting condition: * Python version: Python 3.6.3 :: Anaconda, Inc.About computer: MacBook Pro (13-inch, 2016, Two Thunderbolt 3 ports),
2 GHz Intel Core i5, 256GB SSDHypothesis:* Is Cython really fast (compare to other language) ? * How does
Code differences affect performance ?Test process and results as shown below:Recall the speed of Swift: 0m0.416s, Go: 0m0.592s and Pypy: 0m2.633sTest pure python code:sum=0foreinrange(30):sum=0x=[]foriinrange(1000000):x.append(i)y=[]foriinrange(1000000-1):y.append(x[i]+x[i+1])i=0foriinrange(0,1000000,100):sum+=y[i]print(sum)Speed test result is same/similar to original testtime python test_python.py
9999010000

real    0m12.825s
user    0m11.721s
sys     0m1.061sTest cython code: Create run.py with code:fromcython_npm.cythoncompileimportexportexport('test_cython.pyx')# will do once timeimporttest_cythonCode intest_cython.pyx:cdeflongsum=0cdefinticdefinteforeinrange(30):sum=0x=[]foriinrange(1000000):x.append(i)y=[]foriinrange(1000000-1):y.append(x[i]+x[i+1])i=0foriinrange(0,1000000,100):sum+=y[i]print(sum)Speed test result: time python run.pytime python run.py
9999010000

real    0m5.803s
user    0m4.496s
sys     0m1.211sTest cython code with list optimization and cache: create similar
run.py. Code intest_cythoncache.pyx:fromfunctoolsimportlru_cache@lru_cache(maxsize=128)defdotest():cdeflongmysum=0cdefinticdefinteforeinrange(30):mysum=0x=[iforiinrange(1000000)]y=[x[i]+x[i+1]foriinrange(1000000-1)]i=0foriinrange(0,1000000,100):mysum+=y[i]print(mysum)dotest()Speed test result:time python run.py
9999010000

real    0m3.373s
user    0m2.360s
sys     0m1.001sTest cython code with cache and C array: create similar run.py. Code
intest_cythoncache.pyx:fromfunctoolsimportlru_cache@lru_cache(maxsize=128)defdotest():cdeflongmysum=0cdefinticdefintecdefintx[1000000]cdefinty[1000000]foreinrange(30):mysum=0foriinrange(1000000):x[i]=i# y = []foriinrange(1000000-1):y[i]=(x[i]+x[i+1])i=0foriinrange(0,1000000,100):mysum+=y[i]print(mysum)dotest()Speed test result:time python run.py
9999010000

real    0m0.085s
user    0m0.067s
sys     0m0.015sConclusionsWith a slight change, Cython make pure python code faster by 2X time.
But it is very slow compare to Swift and GoAppling some optimal technical, Cython make python nearly 4X time
faster than the original code. It may be the acceptable result. Pypy
result seems very attractive too.Using C array, Cython make the code become very fast. It consumes
only 0.085s to complete as 4X time faster than Swift, 6X time faster
than Go. It maybe the fastest but it is unusable in real life.After all, i wish cython and cython_npm could give you more usefull
options in coding