JobConsumer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 4 1
A run() 0 12 5
1
#!/usr/bin/env python
2
3
import sys
4
5
import multiprocessing
6
import platform
7
import threading
8
try:
9
    import Queue as queue
10
except ImportError:
11
    import queue
12
import timeit
13
14
import tcod
15
#import libtcodpy as tcod
16
17
THREADS = multiprocessing.cpu_count()
18
19
MAP_WIDTH = 100
20
MAP_HEIGHT = 100
21
MAP_NUMBER = 500
22
23
PATH_NUMBER = 500
24
25
class JobConsumer(threading.Thread):
26
27
    def __init__(self, i):
28
        threading.Thread.__init__(self)
29
        self.daemon = True
30
        self.astar = tcod.path_new_using_map(maps[i])
31
32
    def run(self):
33
        while 1:
34
            job, obj = jobs.get()
35
            if job == 'fov':
36
                tcod.map_compute_fov(obj, MAP_WIDTH // 2, MAP_HEIGHT // 2)
37
            elif job == 'astar':
38
                tcod.path_compute(self.astar,
39
                                  0, 0, MAP_WIDTH - 1 , MAP_HEIGHT - 1)
40
                x, y = tcod.path_walk(self.astar, False)
41
                while x is not None:
42
                    x, y = tcod.path_walk(self.astar, False)
43
            jobs.task_done()
44
45
maps = [tcod.map_new(MAP_WIDTH, MAP_HEIGHT) for i in range(MAP_NUMBER)]
46
jobs = queue.Queue()
47
threads = [JobConsumer(i) for i in range(THREADS)]
48
49
def test_fov_single():
50
    for m in maps:
51
        tcod.map_compute_fov(m, MAP_WIDTH // 2, MAP_HEIGHT // 2)
52
53
def test_fov_threads():
54
    for m in maps:
55
        jobs.put(('fov', m))
56
    jobs.join()
57
58
def test_astar_single():
59
    astar = tcod.path_new_using_map(maps[0])
60
    for _ in range(PATH_NUMBER):
61
        tcod.path_compute(astar, 0, 0, MAP_WIDTH - 1 , MAP_HEIGHT - 1)
62
        x, y = tcod.path_walk(astar, False)
63
        while x is not None:
64
            x, y = tcod.path_walk(astar, False)
65
66
def test_astar_threads():
67
    for _ in range(PATH_NUMBER):
68
        jobs.put(('astar', None))
69
    jobs.join()
70
71
def main():
72
    for m in maps:
73
        for y in range(MAP_HEIGHT):
74
            for x in range(MAP_WIDTH):
75
                tcod.map_set_properties(m, x, y, True, True)
76
77
    for thread in threads:
78
        thread.start()
79
80
    print('Python %s\n%s\n%s' % (sys.version, platform.platform(),
81
                                   platform.processor()))
82
83
    print('\nComputing field-of-view for %i empty %ix%i maps.' %
84
          (len(maps), MAP_WIDTH, MAP_HEIGHT))
85
    single_time = min(timeit.repeat(test_fov_single, number=1))
86
    print('1 thread: %.2fms' % (single_time * 1000))
87
88
    multi_time = min(timeit.repeat(test_fov_threads, number=1))
89
    print('%i threads: %.2fms' % (THREADS, multi_time * 1000))
90
    print('%.2f%% efficiency' %
91
          (single_time / (multi_time * THREADS) * 100))
92
93
    print('\nComputing AStar from corner to corner %i times on seperate empty'
94
          ' %ix%i maps.' % (PATH_NUMBER, MAP_WIDTH, MAP_HEIGHT))
95
    single_time = min(timeit.repeat(test_astar_single, number=1))
96
    print('1 thread: %.2fms' % (single_time * 1000))
97
98
    multi_time = min(timeit.repeat(test_astar_threads, number=1))
99
    print('%i threads: %.2fms' % (THREADS, multi_time * 1000))
100
    print('%.2f%% efficiency' %
101
          (single_time / (multi_time * THREADS) * 100))
102
103
if __name__ == '__main__':
104
    main()
105