![]() |
Using TStaticVar for Faster Scripts
The Problem
If you have a big array, and you want to find something in it, you can do something like this: PHP Code:
The main problem is that if temp.bigArray gets even bigger, someNumber in temp.bigArray will take even longer. The way in works is that the GS engine does a loop until it finds what you are looking for, then it stops. So if what you are looking for happens to be at the end of a really big list (or not in the list at all), you could be in trouble, because it'd need to go through the whole list to find it. Oh no, I'm doomed now! Or am I? Thankfully, there is a way to make this faster in GS2, because Stefan has implemented a "hash map". What this means is that, no matter how big your data is, it will always take the same amount of time to find something. The way we can harness this power is with a TStaticVar. Here is some code: PHP Code:
No matter how big your temp.bigHash is, you will always be able to find your number in the same amount of time! If you are wondering how to do PHP Code:
PHP Code:
The only downside to this approach is that a TStaticVar will take slightly longer for the engine to make than an array would. This means that a good place to use a TStaticVar is when you need to look up a couple things at once or infrequently update the hash. The Evidence Without this, I'd just be some crazy guy blabbing on about stuff. Here are the facts! I'll be using my GBench script to take get these statistics since its makes everything really easy. This is the code to run (and change the number based on how many we are trying). PHP Code:
Starting small, let's fill up an array with 10 things, and a hash with 10 things. Then we'll try to find the last (tenth) item each and see which takes longer. And the results... NPC Code:BigArray: 0.001807276 ms Oh no, looks like the array was a bit faster for this one... If you are thinking now that it's going to require thousands of elements to see a speed increase from the hash, you are wrong. Let's try 30: NPC Code:BigArray: 0.00200592 ms It's a small increase, but this proves that the TStaticVar is already working at a list size of only 30! Let's crank it up a notch to 100... NPC Code:BigArray: 0.003129381 ms It's getting really clear now that the hash is a lot faster when your list is bigger. 1000: NPC Code:BigArray: 0.016829137 ms This means at 1000 elements, the hash is almost 10x as fast as the array. It gets even better if you go higher, but I'm not sure how many people work with arrays that big. Final Thoughts In general, this would be pointless if you are already looping through the list to say, add up all the numbers or something. But, if you are just trying to see if something exists in a set of data, using the TStaticVar approach will be a sure way to guarantee that your system will not slow down once you get more data. |
Good post!
|
Interesting, I may consider using it! :)
|
| All times are GMT +2. The time now is 07:35 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.