Optimization of the region configuration file

Source: Internet
Author: User
Tags fread

Some of the previous arrays have written a profile optimization for the region, colleagues mentioned today, close-up together to communicate!
problem Description: Some apps need to show regional information for multiple businesses, but reading from the database or cache is a bunch of area IDs that can't be displayed directly, need to read the region profile to convert the ID to the region name, and the region configuration (area.php) file is larger (122K), It takes about 7ms to load the file each time (the normal SQL read database takes less than 1ms time, which consumes 5ms and is simply intolerable)
Workaround: convert the region-configured PHP file to a plain-text file, read only the required lines at a time, and do not read all the lines; time complexity: O (1)
functions used: Fopen,fseek, Fread
The specific implementation process:
First step: Convert the region profile to a text file
1: Complement the province, city, and district associative arrays, such as the city's array:

Array (' 1 ' = ' Shanghai ', ' 3 ' = ' Beijing ', ' 4 ' = ' Zhengzhou ')

After the completion of the complete change:

Array (' 1 ' = ' Shanghai ', ' 2 ' = = ', ' 3 ' = ' Beijing ', ' 4 ' = ' Zhengzhou ')

2: Write the name of the province to text, one line at a
Format of writing:

Line number   province, city, district name 1            Beijing                                   2            Shanghai                                       3            Tianjin 4            Chongqing 5            Hong Kong             ...          Shanghai Beijing ...            . 753 the county name with ID 1 754 the county name with ID 2 755 the county name with the      ID 3           ...

At this point, the province, city, and district ID and line number will have the following association:

Province: Lineid = ID City: Lineid = id + 34 Zone: Lineid = ID + 34 + 718

At this point, we can get a line number according to the ID of a province, city and district, and then read this line with Fread to get the desired result;

Step two: Optimize the first step structure and read the data from the text
After completing the first step, we began to search the manual to find the function of reading the contents of the specified line of the file, looking for a long time, unfortunately found no such function, it is really in despair, and blind to engage.
A bag of smoke Kung Fu, finally want to understand, the file, it must be a disk, you let it read the 100th line of content, it also does not know the 100 rows of the disk "offset" is how much, can not read it all at once is taken for granted; At this point, suddenly, if the disk "offset" 100th row is a fixed value It should not be a problem to read it directly. Ah, very simple, set the number of bytes per row to a fixed value (assuming that each line is 48 bytes, less than 48 of the filled white space character), the nth line of "offset" is fixed. such as: read 100 lines of content, directly using the Fseek function to move the file internal pointer to 100 * 48 position, and then use the Fread function to read a line of content.
function has been basically done, the functions used are: fopen,fseek,fread;

The third step: some optimization of the program

      character writes a file, uses the pack function to convert the character to a fixed byte length; the content to be written to each line is optimized: The province name row is unchanged, the city name row adds the province's information except the storage city name, the county name row appends the city, the province information; At this time, the province name line fixed length 48 bytes, the city row District row fixed length 144; The length, province, city, and region of each part of the province, city, and region are stored in the first row of the file, and the queried class uses singleton mode to prevent duplicate queries for the queried ID storage;

The file storage format changes to:

0 The       first line stores the basic information, such as the number of provinces, the province name of the number of bytes per line, etc. 1       Beijing *******                                  2       shanghai *******                                     3       Tianjin *******4       Chongqing *******5       Hong Kong ******* ...      Shanghai, Shanghai *****************36      *************************37      Beijing, Beijing *****************38      Zhengzhou, Henan * * * * *************.        ..        ..        . 753     ID 1 county name, ID 1 County city, the province name ***********754     ID 2 county name, ID 2 County city, the province name ***********755     ID 3 of the county name, ID 3 County city, province name *********** ...        

  

Performance testing
Code:

<?phpinclude "area.php";//randomly generate 20 idfor ($i = 1; $i < 20000; $i + +) {        $id [] = rand (1,3000);} $stime = Microtime (True), foreach ($id as $v) {        $info = xz_area::getinstance ()->getareanav ($v, ' area ');} $etime = Microtime (true); echo "\ n spend time:". (($etime-$stime) * 1000). " Ms\n ";

Results: Average hard disk execution time is 0.5ms; SSD disk average execution time is less than 0.3ms

Optimization of the region configuration file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.