Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Created November 6, 2018 23:45
Show Gist options
  • Save chelseatroy/ed7755ea9e36254a54067acb20fe24dc to your computer and use it in GitHub Desktop.
Save chelseatroy/ed7755ea9e36254a54067acb20fe24dc to your computer and use it in GitHub Desktop.
Calls with Cython
import ctypes
import time
from _ctypes import Structure, POINTER, byref
from ctypes import c_int, c_float
import pyximport; pyximport.install()
import multiplication
import numpy as np
a = np.random.rand(2000,2000)
b = np.random.rand(2000,2000)
print("Timing python multiplication:\n")
start = time.time()
python_result = python_multiplication(a, b)
end = time.time()
print(end - start)
print("Timing ctypes C multiplication:\n")
c_result = c_multiplication(a, b)
print("Timing cython multiplication:\n")
start = time.time()
python_result = multiplication.cython_multiplication(a,b)
end = time.time()
print(end - start)
print("Timing numpy multiplication:\n")
start = time.time()
python_result = a * b
end = time.time()
print(end - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment