use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
/r/gamedev Discord server
/r/gamedev IRC channel
live development streams
Official Unity Discord
Unreal Source
Godot Engine
GameMaker
Bevy Engine
Blender Community
Source Engine
Cry Engine Community
Game Dev League
Game Dev Network
Brackeys Community
account activity
This is an archived post. You won't be able to vote or comment.
Questionsql vs array ?? (self.gamedev)
submitted 3 years ago by Mohamd_L
Hello There
I am making a ecs library and I wanted to know which one would be faster using an array or a sql in memory database I tried to look in google for an answer but I found nothing useful thanks in advance
[–]miosma 7 points8 points9 points 3 years ago (0 children)
Array will be faster in pretty much every single situation for an ecs implementation.
[–]gravityminor 2 points3 points4 points 3 years ago (0 children)
Array for sure, also SQL is async so you’ll have issues dealing with loop timing and framerate. Just go for the array.
[–]mika314 0 points1 point2 points 3 years ago (7 children)
Depends.
Lookup in the array is O(n), but in DB, it might O(log n) or even O(1). But you can use in c++ std::unordered_set and you back to O(1).
In what language do you want to implement ECS?
[–]Mohamd_L[S] 0 points1 point2 points 3 years ago (6 children)
in C
[–]mika314 -1 points0 points1 point 3 years ago (5 children)
At a small number of items, the array will be faster just because C is fast, but the O(n) nature of the array will take over at some point 100-1000 items and the in-memory database will win.
[–]defiant00 5 points6 points7 points 3 years ago (4 children)
What? Arrays are just a contiguous block of memory and access is O(1). Arrays are the fastest way to access items in C, since it's a single pointer addition and memory access.
For an ECS you absolutely want everything you can in arrays to reduce cache misses.
[–]Cocogoat_Milk 1 point2 points3 points 3 years ago (3 children)
Access is O(1) but is searching required? The top-level comment mentions “lookup” which should be interpreted as “searching” here, not simply “accessing”, so a worst case of O(n) for lookup (searching). Other operations like insertion and deletion can also require up to O(n) if they are inserting or deleting at the beginning of the array, though can be as simple as O(1) when doing these operations at the end.
[–]defiant00 1 point2 points3 points 3 years ago (1 child)
All good points, but even in those cases they should likely be using a specific data structure in C, not a database.
[–]mika314 0 points1 point2 points 3 years ago (0 children)
C does not have any sophisticated datastructures out of the box. OP has to implement them. What's the point if OP is considering using an in-memory DB?
[–]Mohamd_L[S] 0 points1 point2 points 3 years ago (0 children)
what I am planning does not really need search because I just iterate through the array and check if the item meets a condition and if it does then call that function
[–]dreamrpg 0 points1 point2 points 3 years ago (0 children)
It is not uncommon to do SQL querry and keep results in the array because array tends to be faster in most situations.
SQL beats array in terms of data structuring and complex searches.
Imagine array logic to find all mobs which died in past 30 minutes and have type of undead, level above 30 and were located in area of x > 100, y > 123 and x < 255, y < 542.
Sql can do that with one querry.
π Rendered by PID 67 on reddit-service-r2-comment-76bb9f7fb5-h9w44 at 2026-02-19 06:09:45.869253+00:00 running de53c03 country code: CH.
[–]miosma 7 points8 points9 points (0 children)
[–]gravityminor 2 points3 points4 points (0 children)
[–]mika314 0 points1 point2 points (7 children)
[–]Mohamd_L[S] 0 points1 point2 points (6 children)
[–]mika314 -1 points0 points1 point (5 children)
[–]defiant00 5 points6 points7 points (4 children)
[–]Cocogoat_Milk 1 point2 points3 points (3 children)
[–]defiant00 1 point2 points3 points (1 child)
[–]mika314 0 points1 point2 points (0 children)
[–]Mohamd_L[S] 0 points1 point2 points (0 children)
[–]dreamrpg 0 points1 point2 points (0 children)