• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

求一利用php快速生成柱状图的函数,现以完成一个简易函数比较慢。该如何解决

php 搞代码 3年前 (2022-01-24) 13次浏览 已收录 0个评论

求一利用php快速生成柱状图的函数,现以完成一个简易函数比较慢。
求一利用php快速生成柱状图的函数,现以完成一个简易函数比较慢。

由于初学,仅会操控CSS生成个简易的柱状图,但我的函数在数据大于25000条的时候超级慢,1万多一点的时候生成图的时间是10多秒。这速度太慢了,求大侠优化。

PHP code

<!---ecms Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><style type="text/css">.chart {    font-family: Tahoma;    font-size: 12px;    border: 1px solid #ccc;    width:auto;    float: left;    margin: 0;    padding: .4em .1em;    height:auto;}.chart ul {            margin:0px;    height:auto;    text-align: center;    display:block;    background-repeat: no-repeat;    background-position: center bottom;    background-attachment: fixed;}.chart li {    list-style: none;    float: left;    width: 35px;    height:200px;    text-align: center;    display:block;    background-repeat: no-repeat;    background-position: center bottom;    background-attachment: fixed;}.b {    display: block;    width:15px;    background-color:#000;    margin-right: auto;    margin-left: auto;    }.a {        height:12px;    width:35px;    background-color:#fff;                padding: 0px;}</style><?php $beg_time = microtime(true);$beg_time *= 10<mark style="color:transparent">本文来源gaodaimacom搞#^代%!码&网*</mark><pre>搞代gaodaima码

00 * 1000;create_chart2();function create_chart2() {//x一维数组直接输出图表 //思路先得到一个一维数组,然后统计值的个数,最后排序输出 //echo ‘

‘; $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 0,1000′; $res1=$db1->query($sql_query); while($row1=mysqli_fetch_array($res1,MYSQLI_NUM)) { $returnarray2=array_merge($returnarray2,$row1); } $array_values=array_count_values($returnarray2); ksort($array_values); //print_r($array_values); $rates=round(200/max($array_values),2); echo ‘$rates=’.$rates; echo ‘

一区

    ‘; 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; ?>

——解决方案——————–
先找出瓶颈是在什么地方

有没考虑过使用 pChart?
——解决方案——————–
Google有一个库提供。楼主可以查一下。
——解决方案——————–
输出的 Total time used 看似巨大,但由于计量单位有误,就不足信了
microtime(true) 返回的是秒, * 1000 * 1000 后为微秒,你把它当做毫秒,就放大了 1000 倍

代码简单并不代表运行效率就高,看一下这个测试就知道了

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;
------解决方案--------------------
建议楼主看看:ChartDirector软件,现在有破解的,很好用


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:求一利用php快速生成柱状图的函数,现以完成一个简易函数比较慢。该如何解决
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址