Slow Performance and cpu usage

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nthrack
:warning: Old Version Published before Godot 3 was released.

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!

About the cpu-usage: Maybe you have a quad-core cpu? Your code is single-threaded, so naturally it can only fully use one core (25% usage on a 4-core machine)

Hinsbart | 2016-07-07 18:11

Ty Hinsbart,

you are right, my machine has 4 core, this explains the 25% of cpu usage.

i will try to re-implement my code and try to use more threads.

Thanks again.

nthrack | 2016-07-08 13:41