Skip to content

Commit

Permalink
Add sample 04 and desc more.
Browse files Browse the repository at this point in the history
  • Loading branch information
kzky committed Aug 27, 2016
1 parent 49bd7ad commit 900139c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cython/nogil/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@ Elapsed time (ave) 2.09492807388 [s]
Elapsed time (std) 0.304449698912 [s]
```

In both stats, cython is faster and more stable.
In both stats, cython is faster and more stable. Note that the concatenation of results comming from each thread is a bottle neck. [main_multi_thread04.py](./main_multi_thread04.py) shows that if I comment the *Y = np.concatenate(Y)*.

### Codes
- [multi_thread03.pyx](./multi_thread03.pyx)
- [main_multi_thread03.py](./main_multi_thread03.py)
- [main_multi_thread04.py](./main_multi_thread04.py)

## Conclusion

Seeing results among the experients, the result of numpy were fluctuating even if using the same code while Cython codes gradually speed up according the step-by-step optimization.
Seeing results among the experients, the result of numpy were fluctuating even if using the same code while Cython codes gradually speed up according the step-by-step optimization; however the results reported above may change in time and environement.

*Cython + threading + nogil* is a best choise based on my experience in some viewpoints, e.g., easiness to start using and to write, memory efficiency, computational speed. However for Numpy, there are some room to speed up, i.e., using MKL.

Expand Down
45 changes: 45 additions & 0 deletions cython/nogil/main_multi_thread04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pyximport; pyximport.install()
from multi_thread03 import *
import numpy as np
import time

def main():

X = -1 + 2*np.random.rand(7**2 * 512 * 4096)
n = 10

print("# Numpy")
t = []
for _ in range(n):
st = time.time()
Y = array_f(X)
et = time.time()
t.append(et - st)
print("Elapsed time (ave) {} [s]".format(np.mean(t)))
print("Elapsed time (std) {} [s]".format(np.std(t)))

print("# Cython")
t = []
for _ in range(n):
st = time.time()
q = len(X) // 4
range_ = range(0, len(X), q)
threads = []
for i in range_:
tt = TaskThread(X[i:i+q])
tt.start()
threads.append(tt)

Y = []
for tt in threads:
tt.join()
Y.append(tt.y)
#Y = np.concatenate(Y)
et = time.time()
t.append(et - st)

print("Elapsed time (ave) {} [s]".format(np.mean(t)))
print("Elapsed time (std) {} [s]".format(np.std(t)))

if __name__ == '__main__':
main()

0 comments on commit 900139c

Please sign in to comment.