No one answer me, me is sad very too much :'(
I do some simple data digestion and the answer is no. The reason behind about this, I still don't know.
The data show me that use thread is always worse than normal loop up to about 4 times.
Still if I use it to calculate many frames(not in single frame), the performance is still much better since there is no freeze.
const SIZE = 10000000
const THREAD = 4
var a = PoolByteArray()
var b = PoolByteArray()
var base
var seeds = []
var rand = []
var thread = []
var timer
func _ready():
# prepare data
a.resize(SIZE)
b.resize(SIZE)
randomize()
base = randi()
seed(base)
# show sample data
print("Data size is ", SIZE)
print("Thread count is ", THREAD)
print("Sample seed is ", base)
# initialize random seed
for i in range(THREAD):
seeds.append(randi())
rand.append(RandomNumberGenerator.new())
rand[i].seed = seeds[i]
# Set A, normal loop. To ensure both loop type return same result
# At same index, use same rng seed
timer = OS.get_system_time_msecs()
for i in range(SIZE):
a.set(i, rand[i % THREAD].randi() % 256)
print("Set A complete in ", OS.get_ticks_msec(), " ms")
# Reset seed
for i in range(THREAD):
rand[i].seed = seeds[i]
# Set B, thread loop
timer = OS.get_ticks_msec()
for i in range(THREAD):
var t = Thread.new()
t.start(self, "_loop", i)
thread.append(t)
for t in thread: # Wait for every thread to finish
t.wait_to_finish()
print("Set B complete in ", OS.get_ticks_msec(), " ms")
# To ensure if both result is same
if a.hex_encode() == b.hex_encode():
print("Set A and Set B is match")
else:
print("Set A and Set B is not match")
func _loop(start):
for i in range(start, SIZE, THREAD):
b.set(i, rand[start].randi() % 256)
