Evaluate a function that uses php to quickly generate a bar chart. it is slow to complete a simple function. How to solve

Source: Internet
Author: User
Tags shuffle
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.

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.