C++ create Nodes slower than gdscript. What I am doing wrong?

Godot Version

4.2.1

Question

Hi guys. Please help me understand what’s wrong with my code. I have this loop in gdextension. It’s just creating new Node2D and adding as a child.
The issue is, that C++ is performing it about 19 seconds for 400,000 nodes, and the same code in gdscript doing the same thing in 1.4 seconds.
I don’t expect C++ to perform faster in this case, as it calls the same method that is written in c++ and used in gdscript. But shouldn’t it be somewhat near to the gdscript speed?

Here is the gdextension code

void FastIterator::loop(const int size) {
	for (int i = 0; i < size; i++) {
		Node2D* sprite = new Node2D();
		this->add_child(sprite);
	}
}

And here is gdscript

func iterate(size: int) -> void:
	for i in range(size):
		var _node = Node2D.new()
		add_child(_node)

I have fixed this issue. But I am not sure why it was slow before.
So I was adding new created Nodes as a child to my GdExtension node. This was slowing down the process.
Now I am passing new Node from gdscript and adding childs to that node. By doing this now it takes 1.3 seconds instead of 1.4 seconds of gdscript (I think c++ for loop just gives that 0.1 second buff).
If anybody knows why it was slow to add new Node2D as a child to GdExtension node please explain to me.
Thank you

here is the updated code.

void FastIterator::loop(const int size, Variant par) {
	Node2D* nods = memnew_arr(Node2D, size);
	Node2D* p = Object::cast_to<Node2D>(par);

	for (int i = 0; i < size; i++) {
		p->add_child(&nods[i]);
	}
}
extends FastIterator
@onready var node_2d = $"../Node2D"

func _ready():
	var time_start = Time.get_ticks_msec()
	loop(400000, node_2d)
	print(Time.get_ticks_msec() - time_start)

I assume it was passing the data back and forth every time before and slowing it down.
As far as I know though you don’t want to be interacting with the scene tree at all if you want this to be as fast as possible. You should be using the servers directly instead. Optimization using Servers — Godot Engine (stable) documentation in English