Menu

[025cc2]: / Samples / SampleThreaded.py  Maximize  Restore  History

Download this file

58 lines (41 with data), 1.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Shows how to use threads to speed up resource loading."""
import time
import d3d11
import d3d11x
from d3d11c import *
message = """Comparison between normal texture loading and threaded loading:
Normal: %.3f seconds
Threaded: %.3f seconds
Threaded version was ~%.2f times %s.
"""
class SampleThreaded(d3d11x.Frame):
def onCreate(self):
self.window.show(SW_HIDE)
textureName = d3d11x.getResourceDir("Textures", "static-barrel.dds")
#Warm the filesystem cache.
dummy = d3d11.Texture(textureName)
COUNT = 10
#Force format conversion, heavy work for the CPU.
FORMAT = FORMAT_R8G8B8A8_UNORM
#Normal loading.
normalStart = time.clock()
for x in range(COUNT):
t = d3d11.Texture(textureName, (0, 0, 1, 0), FORMAT)
normalTime = time.clock() - normalStart
#Threaded loading.
threadStart = time.clock()
loader = d3d11x.ThreadedLoader()
for x in range(COUNT):
loader.addJob(self, "t", d3d11.Texture, textureName, (0, 0, 1, 0), FORMAT)
#loader.wait()
loader.join()
threadTime = time.clock() - threadStart
comp = normalTime / threadTime
result = "faster"
if comp < 1.0:
result = "slower"
d3d11.messageBox(self.window, message % (normalTime, threadTime, comp, result), "Results", 0)
self.exit()
if __name__ == "__main__":
sample = SampleThreaded("Threading - DirectPython 11", __doc__)
sample.mainloop()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.