Hacker News new | past | comments | ask | show | jobs | submit login
Speeding up Python with Nim (robert-mcdermott.gitlab.io)
27 points by mcdermott on Jan 21, 2019 | hide | past | favorite | 5 comments



Hmm. this didn't render the newlines in my code, and markdown doesn't seem to be supported. bummer.

Really cool! It seems to be comparable and perhaps even faster than cython!

I happened to be in an Ipython shell and didn't see anyway to do this with nim, but you can probably get a good approximate of the speedup with nim in Ipython/Jupyter by doing a quick:

[1] %load_ext Cython

[2] %%cython ``` cpdef long cyfib(int n): if n <= 2: return 1 else: return cyfib(n - 1) + cyfib(n - 2) ```

[3] start=time.time(); x = cyfib(47); end=time.time(); print(x); print(end-start)

2971215073 5.558561325073242

Nice choice of 47! I had to change the cython function's return time to long to prevent overflow. Something you don't need to do in nim


Nice writing. Using python as a 'glue' lang and nim as a 'core' lang makes a lot of sense. Will give it a try for my next project.


You also can use lots of python functions directly in nim too. Do this in nim:

import nimpy; let datetime = pyImport("datetime"); echo datetime.datetime.now(); let pysys = pyImport("sys") ; echo pysys.version;

It is really cool.


The complexity is of recursive implementation of fibonacci is O(golden ratio ^ n). If it was O(n ^ 2) it would run under 0.1s.

By the way, the nimpy looks cool. Thanks for sharing.


This was an excellent read. I'll have to take a deeper look at the NimPy project that was mentioned in the article. Thanks for sharing.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: