| 1 |
import unittest |
| 2 |
import time |
| 3 |
import random |
| 4 |
import jpyutil |
| 5 |
jpyutil.init_jvm(jvm_maxmem='512M') |
| 6 |
import jpy |
| 7 |
|
| 8 |
|
| 9 |
class TestPerformance(unittest.TestCase): |
| 10 |
|
| 11 |
def test_general_rt_perf(self): |
| 12 |
|
| 13 |
Integer = jpy.get_type('java.lang.Integer') |
| 14 |
String = jpy.get_type('java.lang.String') |
| 15 |
File = jpy.get_type('java.io.File') |
| 16 |
HashMap = jpy.get_type('java.util.HashMap') |
| 17 |
|
| 18 |
|
| 19 |
N = 1000000 |
| 20 |
|
| 21 |
indexes = list(range(N)) |
| 22 |
random.shuffle(indexes) |
| 23 |
|
| 24 |
t0 = time.time() |
| 25 |
pairs = [(Integer(index), File('path')) for index in indexes] |
| 26 |
t1 = time.time() |
| 27 |
print('Integer + File object instantiation took', t1-t0, 's for', N, 'calls, this is', 1000*(t1-t0)/N, 'ms per call') |
| 28 |
|
| 29 |
map = HashMap() |
| 30 |
|
| 31 |
t0 = time.time() |
| 32 |
for pair in pairs: |
| 33 |
i, f = pair |
| 34 |
map.put(i, f) |
| 35 |
t1 = time.time() |
| 36 |
print('HashMap.put() took', t1-t0, 's for', N, 'calls, this is', 1000*(t1-t0)/N, 'ms per call') |
| 37 |
|
| 38 |
t0 = time.time() |
| 39 |
for pair in pairs: |
| 40 |
i, f = pair |
| 41 |
f = map.get(i) |
| 42 |
t1 = time.time() |
| 43 |
print('HashMap.get() took', t1-t0, 's for', N, 'calls, this is', 1000*(t1-t0)/N, 'ms per call') |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
if __name__ == '__main__': |
| 48 |
print('\nRunning ' + __file__) |
| 49 |
unittest.main() |