Performance analysis of Strtotime function in PHP

Source: Internet
Author: User
Recently in a game statistics background, the most basic function is to analyze the registration log to display user data. In-house testing, the number of users is very small, so there is no performance problems found. But these two days together in the real test environment, the user volume of miso to come in, starting from the afternoon, the number of online statistics start card, a few seconds to return data; In the evening, statistics on the number of online people basically load time-out can not open. Although I do not know what their game side of the bug, the player there is often a problem login, resulting in the number of online and registration is not many. But the amount of data on this side of my query speed is also not good, it is very embarrassing.

Now they are looking at the game bug, my side is also watching the statistics behind the code in the end where the performance. First of all, I counted the data is from the library, they play with the main library, and my side of the number of administrators, it is impossible to affect the performance of the game wear problem.

Today, the project leader leads the database to the server within the company. I cuffed a copy to this machine to see where the performance of the statistical platform is. Then found that even registered statistics are very card, the server is about two seconds to return, this confidential more than 20 seconds, but also often time-out (PHP default configuration is 30 seconds time-out), on-line statistics will not be sure to open. Looked at the database, the registration record of the day is about 3,500 (there is false data), every five minutes statistics, one day is counted 288 times. Of course, this is certainly not a circular query database 288 times, that will be scolded dead bar.

Count the number of registrations in the time period, logic is very simple, that is, each time period to traverse the data, compare the time size, in accordance with + 1. But why so simple logic, also 1 million cycles, how can run out a full half a minute of time so long?

The key problem lies in the time comparison here, we all know that time stamp is a relatively scientific method of comparing time, and the time recorded in the database is generally in the form of Yyyy-mm-dd HH:ii:ss, PHP has strtotime function converted to timestamp. However, after 288 for * 3,500 foreach Plus, the execution time is up to half a minute.

$nowDayDT = strtotime (Date (' y-m-d ')), $__startt = Microtime (TRUE), for ($i =0; $i < $allTime; $i + = $gapTime) {  $count = 0;  $startDT for data Comparison  = $nowDayDT + $i;  $endDT = $nowDayDT + $i + $gapTime;  $xAxis for display  1 = date (' h:i ', $nowDayDT + $i);  $xAxis 2 = date (' h:i ', $nowDayDT + $i + $gapTime);   foreach ($rawData as $line) {    $time = strtotime ($line [' Log_dt ']);    if ($startDT <= $time && $time < $endDT) {      $count + +;    }  }  $RESARR [] = [    ' date ' = = $xAxis 1. ' ~ '. $xAxis 2,    ' number ' = '  $count];} echo microtime (TRUE)-$__startt;

In that case, basically there is no way to use this strtotime function, then there is any way to compare the size of time? The answer is simple and rude, PHP can directly compare two date time string! So the code after the change is as follows. And now it's running for about 0.3 seconds.

$__startt = Microtime (TRUE); for ($i =0; $i < $allTime; $i + = $gapTime) {  $count = 0;  $startDT for data comparison  = Date (' y-m-d h:i:s ', $nowDayDT + $i);  $endDT = Date (' y-m-d h:i:s ', $nowDayDT + $i + $gapTime);  $xAxis for display  1 = date (' h:i ', $nowDayDT + $i);  $xAxis 2 = date (' h:i ', $nowDayDT + $i + $gapTime);   foreach ($rawData as $line) {    $time = $line [' Log_dt '];    if ($startDT <= $time && $time < $endDT) {      $count + +;    }  }  $RESARR [] = [    ' date ' = = $xAxis 1. ' ~ '. $xAxis 2,    ' number ' = '  $count];} echo microtime (TRUE)-$__startt;

Traverse re-optimization

You may find a problem, for nested within a foreach, this performance is a bit worrying, where the inside of the foreach is necessary to completely traverse it? Actually, it doesn't have to be. Just check the SQL data and sort it out by time. The optimized time comparison algorithm is as follows:

for{. foreach ($rawData as $line) {  $time = $line [' Log_dt '];//strtotime ($line [' Log_dt ']);  The optimization algorithm calculates  if ($time < $startDT) continue;  Skips the  if ($time >= $endDT) Break if it is less than the start time;    The end time is greater than  $count + +;            Otherwise eligible  //original algorithm//  if ($startDT <= $time && $time < $endDT) {//    $count ++;//  }} ...}

The Continue and break keywords are used here to skip a loop and end the entire loop. This time, in the beginning of the day, a large part of the data can be skipped directly. The final total traversal time is reduced to about 0.12 seconds.

Summary, in large-scale data processing, should try to avoid in the traversal of data conversion, to avoid the use of some complex functions of the principle. such as Strtotime

  • 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: 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.