20.05.2015, 07:47
(
Последний раз редактировалось Yashas; 23.05.2015 в 09:57.
)
When to use INI and when not
Introduction
First of all , every SA-MP programmer needs to know one important thing, PAWN is lot lot lot slower than natives. Since every INI Reader does most of the work in plain PAWN code , it would be very slow.I see many budding SA-MP programmers using INI Files to store their player database though there are much faster methods available such as SQLite,MySQL,etc.This whole tutorial is dedicated to those who still make use of INI to store large chunks of information including player database.This little tut will give an idea when and when not to use INI.
Before we begin, go through the following conversation.
Was that mind blowing? o.O
As I have already said INI Readers make heavy use of PAWN code with all kinds of loops and...therefore INI Readers will be extremely slow when compared with SQLite(SA:MP natives) or MySQL(plugin).
I wonder who was the first guy who started using INI is store a player database.The sole purpose of INI when invented was to store settings for an application.
How does an INI Reader work?
Since I myself have written an INI Processor I have a very good picture of what happens when you read data from INI files.Every INI processor first opens the file then stores all the content in a huge buffer.It then parsers the whole file.You then call a Read function which will again search the whole cache for the key.Let's see what basically happens when you want to read the contents of this file.
Searching for 'Name' won't take that much time since it is the first key.While searching for 'Score' Key, the INI Reader first checks 'Name' to see if it matches with 'Score' then checks for 'Score'. If you wanted 'Money' then it will have to first check 'Name' then 'Score' and then finally 'Money'.
To read a file with N keys, you will have to do (N(N+1)/2) string comparisons.Not just that, it then has to separate out the key value from the cache which is again very slow.
You might have already realized how slow could this be.Though y_ini and eINI provide an alternative for loading data using callbacks which is more efficient , they are still slow.
Writing information to INI files is more worse.The INI readers will first find the key,then use format lot of times.... in fact writing is 4-10x slower than reading for eINI & y_ini for a small file.
What if you had just edit one key? You need to open the file ,load everything, change the key, make a new file and copy all the data.Wow, isn't it amazing?
When you must NOT use INI?
Since INI Processing takes lot of time, you must not use them when there is data to be loaded or written frequently.For example, a player database.You must also avoid using INI when you have to load huge pieces of information since it could probably lag your server badly sometimes.The server may even freeze for a while.
Here are few situations where you must avoid using INI:
There are many alternatives to INI.I will be mentioning two of them here.
MySQL:
You can use MySQL which is by design is efficient in searching and updating bits of information.Using MySQL also gives you further advantages.You can share information between your server,UCP,website,etc easily.PHP has ready-made functions which allow you to use MySQL.You can even make your own desktop/mobile app for your server which can access information from MySQL.
If you have never heard or used MySQL, here are few nice tutorials to get you started.
Using BlueG's MySQL plugin R7 and newer (with cache)
MySQL plugin ORM usage tutorial
SQLite
This another alternative to INI. SQLite support comes with SA:MP server by default and does not require any additional plugin.
How to use SQLite
If you find that hard, here is ready-made include for making User Database for you.
BUD by Slice
Suggestion by Gammix
It might be a cumbersome task to convert all your code to MySQL or SQLite from INI but its worth taking all the trouble.I am sure that you won't regret later.There are some tools which allow you to convert your code which you will be able to find using the magic button.
When can you use INI?
You can use INI where you are not going to frequently open and close files.I personally use INI Files to store MySQL Configuration and language strings.I load them only once during the server start up so its not a problem.
I hope you now understand when and when not to use INI.
Feel free to suggest something.
Introduction
First of all , every SA-MP programmer needs to know one important thing, PAWN is lot lot lot slower than natives. Since every INI Reader does most of the work in plain PAWN code , it would be very slow.I see many budding SA-MP programmers using INI Files to store their player database though there are much faster methods available such as SQLite,MySQL,etc.This whole tutorial is dedicated to those who still make use of INI to store large chunks of information including player database.This little tut will give an idea when and when not to use INI.
Before we begin, go through the following conversation.
Quote:
Originally Posted by Yashas
I am wondering if native functions are faster than the ones I make.
Код:
for(new i = 400000;i;i--) strcmp(x1,x2); new b = GetTickCount(); printf("SD TST:%d",b-a); a = GetTickCount(); for(new i = 400000;i;i--) { new j =0; while(1) { if(x1[j] != x2[j++]) break; } } My version had to be faster because it cannot be optimized further ,in fact it doesn't even function like strcmp and does less work than strcmp. I had made my own parser which actually checks every character,does all the copying,all comparing,... - So is this bad?I thought I would avoid the function overhead by doing it but.... Thanks |
Quote:
Originally Posted by ******
No, natives are WAY faster than PAWN code! If you found it was twice as fast I would suggest you retest as I would suspect it is far more than that.
|
As I have already said INI Readers make heavy use of PAWN code with all kinds of loops and...therefore INI Readers will be extremely slow when compared with SQLite(SA:MP natives) or MySQL(plugin).
I wonder who was the first guy who started using INI is store a player database.The sole purpose of INI when invented was to store settings for an application.
How does an INI Reader work?
Since I myself have written an INI Processor I have a very good picture of what happens when you read data from INI files.Every INI processor first opens the file then stores all the content in a huge buffer.It then parsers the whole file.You then call a Read function which will again search the whole cache for the key.Let's see what basically happens when you want to read the contents of this file.
Код:
Name=Yashas Score=1234 Money=12345
To read a file with N keys, you will have to do (N(N+1)/2) string comparisons.Not just that, it then has to separate out the key value from the cache which is again very slow.
You might have already realized how slow could this be.Though y_ini and eINI provide an alternative for loading data using callbacks which is more efficient , they are still slow.
Writing information to INI files is more worse.The INI readers will first find the key,then use format lot of times.... in fact writing is 4-10x slower than reading for eINI & y_ini for a small file.
What if you had just edit one key? You need to open the file ,load everything, change the key, make a new file and copy all the data.Wow, isn't it amazing?
When you must NOT use INI?
Since INI Processing takes lot of time, you must not use them when there is data to be loaded or written frequently.For example, a player database.You must also avoid using INI when you have to load huge pieces of information since it could probably lag your server badly sometimes.The server may even freeze for a while.
Here are few situations where you must avoid using INI:
- Player Database
- Houses and other similar stuff
- Loading a file which is half a MB
There are many alternatives to INI.I will be mentioning two of them here.
MySQL:
You can use MySQL which is by design is efficient in searching and updating bits of information.Using MySQL also gives you further advantages.You can share information between your server,UCP,website,etc easily.PHP has ready-made functions which allow you to use MySQL.You can even make your own desktop/mobile app for your server which can access information from MySQL.
If you have never heard or used MySQL, here are few nice tutorials to get you started.
Using BlueG's MySQL plugin R7 and newer (with cache)
MySQL plugin ORM usage tutorial
SQLite
This another alternative to INI. SQLite support comes with SA:MP server by default and does not require any additional plugin.
How to use SQLite
If you find that hard, here is ready-made include for making User Database for you.
BUD by Slice
Suggestion by Gammix
It might be a cumbersome task to convert all your code to MySQL or SQLite from INI but its worth taking all the trouble.I am sure that you won't regret later.There are some tools which allow you to convert your code which you will be able to find using the magic button.
When can you use INI?
You can use INI where you are not going to frequently open and close files.I personally use INI Files to store MySQL Configuration and language strings.I load them only once during the server start up so its not a problem.
- One-Time Reading/Writing
- If data is not read or written frequently
- To store server configuration,settings,etc
I hope you now understand when and when not to use INI.
Feel free to suggest something.