Here's my Python code:
X = [[0] * 1000] * 100
start = time()
for x in xrange(100):
for i in xrange(len(X)):
for j in xrange(len(X[i])):
X[i][j] += 1
print time() - start
My Cython code is the same:
X = [[0] * 1000] * 100
start = time()
for x in xrange(100):
for i in xrange(len(X)):
for j in xrange(len(X[i])):
X[i][j] += 1
print time() - start
Output:
- Python cost: 2.86 sec
- Cython cost: 0.41 sec
Any other faster way to do the same above in Python or Cython?
Update: Any way to create an 2d array X with height indexing performance close to array int X[][] that in C/C++?
For now I am considering use Python C API to do the job.
One more thing, a numpy array does the same thing but so much slower(70 sec) than list in both pure Python and Cython.
Python:
X = np.zeros((100,1000),dtype=np.int32)
start = time()
for x in xrange(100):
for i in xrange(len(X)):
for j in xrange(len(X[i])):
X[i][j]+=1
If doing a lot of access to numeric array, which approach is the best?