Skip to content

Instantly share code, notes, and snippets.

@masnun

masnun/cy.pyx Secret

Last active October 22, 2016 06:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masnun/e0201b10f70fd719edb9b95863b3a3db to your computer and use it in GitHub Desktop.
Save masnun/e0201b10f70fd719edb9b95863b3a3db to your computer and use it in GitHub Desktop.
Cython vs Golang
def do():
cdef long sum = 0
cdef int i = 0
for i in range(2000):
sum += i*i
print(sum)
package main
import "fmt"
func main() {
sum := 0
for i := 0; i < 2000; i++ {
sum += i * i
}
fmt.Println(sum)
}
# The first run would need to compile `cy.pyx` module, so benchmark from the 2nd run
# In real life, we would use setup.py to compile the module into a shared lib before hand
# and we would not need the pyximport stuff. But for quick benchmark, pyximport is more convenient.
import pyximport
pyximport.install()
import cy
cy.do()
➜ playground time go run main.go
2664667000
go run main.go 0.43s user 0.08s system 119% cpu 0.426 total
➜ playground time python main.py
2664667000
python main.py 0.20s user 0.08s system 97% cpu 0.286 total
➜ playground
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment