Why is the Array Find function so fast?

The Array Find function can find an item in the 100,000+ array without affecting fps in the event tick. But when I create a function using ForEachLoop that checks every element and compare to the one I need or while loop, my fps drops to less than 10. Is there any other way to find item in the array that’s really fast?

there’s a lot of things to improve performance in searchs, adding code to the loop = losing performance per loop, also you can search about searching algoritms, checking every item of a 100,000+ array surely will take a lot of time, search about algorithms like “binary search” (it requires that the array is ordered). about the “find” function, i don’t know exactly how it works, so i can’t talk about it specifically, if it’s working so fine, why you don’t use it?

Yeah, I know how to make binary heaps but that Find function can work on any variables like structs so I’m just surprised on its speed when I tested it. I was using 4-ary search on my pathfinding algorithm though it only works on numbers.

the fact that the find function is defined in C++, and your find function probably is made in blueprints usually makes difference, as blueprints have less performance than C++ (hard to say exactly how much C++ is faster than blueprint), besides this, we would need to look at how exactly both functions works to compare.

Yeah I just tested it in ue4 c++ and it’s faster there, I’m probably gonna move to c++ lol, I thought bp wouldn’t have much difference. And in c++ I can loop 1,000,000 array without the engine calling for callstack and even 1,000,000 don’t have an impact in the fps lol, in BP 1000 loop starts having an impact. and 100,000+ loop will call a callstack.

1 Like

C++ imlementation is not much diffrent of what you do in blueprint:

https://github.com/EpicGames/UnrealEngine/blob/76085d1106078d8988e4404391428252ba1eb9a7/Engine/Source/Runtime/Core/Public/Containers/Array.h#L816

ForEachLoop runs in virtual machine and each loop takes longer as it need to do blueprint execution, find function runs nativly on full speed, but still it’s not healthy practice to do it on tick, should be avoided.

remember that you can try to use blueprint nativization if your problem with blueprints is performance. it will try to convert the blueprint code to C++ while packaging.

Newer engine versions have maps in addition to arrays. If you could use a key lookup in place of find, presumably it would be much faster.

good point furroy, i remember Alexander Pascal saying it was faster than arrays when you’re trying to use a large amount of itens.

LOL XDDDDDDDDDDDD

I know this post is like 5 years old, but you can also use TMaps. The find function will always retrieve the correct element. You just need to be able to pair the value with something that can be used to differentiate others. You can use Guids, names, Gameplay tags, etc. And if you really want to get spiffy, you can do this with strings as values and convert the data to a string. There’s a plugin called “AnythingToString” that enables you to use this method for fast and dynamic forms of data storage.

1 Like