- Edited
I wrote a simple test to see if there is a difference in sequential vs threaded performance. On my machine, both tasks run about the same amount of time, threaded is usually a tad slower (14× 12th Gen Intel® Core™ i7-12700H). Why is that?
EDIT: So apparently, running the project in the editor with debug window, has a profound effect on multi threading. This is probably obvious to anyone who has experience with Godot, but me being new to it, it did not occur to me. So I replaced the print statements in the below code with updating a label, so it can be monitored without the debug window showing (i.e. go to project list, select Run; as has been pointed out below, this would still run in debug mode but without the overhead from the editor).
The timings became sensible: Processing 40M iterations sequentially took 430ms on my machine. When distributed among 4 threads, it took 147ms. This is what I would expect.
Thanks everyone for the input!
extends Node
var threads_count: int = 4
var threads_finished: int = 0
var threads = []
var mutex = Mutex.new()
var iters: int = 10_000_000
var start
var finished
func _ready():
# Test sequential processing.
start = Time.get_unix_time_from_system()
for i in threads_count:
for j in iters:
j += 1
finished = Time.get_unix_time_from_system()
print("Sequential: ", finished - start)
# Test parallel processing.
start = Time.get_unix_time_from_system()
for i in threads_count:
threads.push_back(Thread.new())
threads[-1].start(thread_func)
func thread_func():
for i in iters:
i += 1
mutex.lock()
threads_finished += 1
if threads_finished == threads_count:
call_deferred("done")
mutex.unlock()
func done():
for t in threads:
t.wait_to_finish()
threads.clear()
finished = Time.get_unix_time_from_system()
print("Parallel: ", finished - start)