Hello everybody!
I was trying to implement a sort of Priority Queue, in the code below you can see that when i put a element inside the queue i just sort my array.
I already knew that the performance would not be good but I've noticed that while the test is running my cpu usage reach only 25%.
I was expecting something like 90%.
Some one can explain why this is happening ?
I've run the test directly from the editor and these are the results:
Put_Sort of 100 elements: 28ms
Put_Sort of 200 elements: 120ms
Put_Sort of 300 elements: 298ms
Put_Sort of 400 elements: 565ms
Put_Sort of 500 elements: 924ms
Put_Sort of 600 elements: 1420ms
Put_Sort of 700 elements: 2000ms
Put_Sort of 800 elements: 2690ms
Put_Sort of 900 elements: 3416ms
Put_Sort of 1000 elements: 4365ms
My Test Code
extends Node
class PriorityQueue:
var queue
func _init():
queue = []
pass
func sort_priority(v1,v2):
return v1[1] < v2[1]
func empty():
return queue.empty()
func put(vect,priority):
var pv = [[int(vect.x),int(vect.y)],int(priority)]
queue.append(pv)
queue.sort_custom(self,"sort_priority")
func get():
var vect = queue[0][0]
queue.pop_front()
return Vector2(vect[0],vect[1])
func _ready():
for i in range(10):
var pq = PriorityQueue.new()
var ms = OS.get_ticks_msec()
var r=(i+1)*100
for j in range(r):
pq.put(Vector2(j,j),randi() % 100)
print("Put_Sort of ",r," elements: ",OS.get_ticks_msec()-ms,"ms")
Thanks in advance for your help!