Evaluate a function that uses php to quickly generate a bar chart. it is slow to complete a simple function. Evaluate a function that uses php to quickly generate a bar chart. it is slow to complete a simple function. As a beginner, I only use CSS to generate a simple bar chart, but my function is very slow when there are more than 25000 pieces of data, the time for generating an image is more than 10 seconds. This speed is too slow. please optimize it. PHPcode & lt; htm calculates a function that uses php to quickly generate a bar chart. it is slow to complete a simple function.
Evaluate a function that uses php to quickly generate a bar chart. it is slow to complete a simple function.
As a beginner, I only use CSS to generate a simple bar chart, but my function is very slow when there are more than 25000 pieces of data, the time for generating an image is more than 10 seconds. This speed is too slow. please optimize it.
PHP code
'; $ Returnarray2 = array (); $ db1 = new mysqli ('localhost', 'admin', 'admin', 'ssq'); $ SQL _query = 'SELECT l1, l2, l3, l4, l5, l6 from hmk_temp_1 as bb limit '; $ res1 = $ db1-> query ($ SQL _query); while ($ row1 = mysqli_fetch_array ($ res1, events) {$ returnarray2 = events ($ returnarray2, $ row1);} $ array_values = events ($ returnarray2); ksort ($ array_values); // print_r ($ array_values ); $ rates = round (200/max ($ array_values), 2); echo '$ rates = '. $ rates; echo'Zone 1
'; Foreach ($ array_values as $ key => $ value) {echo'
- '. $ Value. ''.''. $ key .'
';} Echo'
'; // Echo'';} $ End_time = microtime (true); $ end_time * = 1000*1000; echo "Total time used :". ($ end_time-$ beg_time ). "ms ". PHP_EOL;?>
------ Solution --------------------
First, find out where the bottleneck is.
Have you considered using pChart?
------ Solution --------------------
Google has a library. You can check it.
------ Solution --------------------
The output Total time used seems to be huge, but the measurement unit is incorrect.
Microtime (true) returns seconds. * 1000*1000 is microseconds. if you treat it as milliseconds, it will be 1000 times larger.
The code is simple and does not mean that the running efficiency is high. let's take a look at this test.
PHP code
$ S_time = microtime (true); $ d = range (1, 33); $ r = array (); for ($ I = 0; $ I <1000; $ I ++) {shuffle ($ d); $ r = array_merge ($ r, array_slice ($ d,-6);} $ r = array_count_values ($ r ); echo microtime (true)-$ s_time, PHP_EOL; $ s_time = microtime (true); $ d = range (1, 33); $ r = array_fill (1, 33, 0); for ($ I = 0; $ I <1000; $ I ++) {shuffle ($ d); foreach (array_slice ($ d,-6) as $ k) $ r [$ k] ++;} echo microtime (true)-$ s_time, PHP_EOL;
------ Solution --------------------
It is recommended that you look at the ChartDirector software, which is currently cracked and easy to use.