如何减少这个脚本的执行时间?

发布于 2024-10-17 07:41:25 字数 8530 浏览 0 评论 0 原文

一个多星期以来,我一直在尝试优化 MySQL 查询脚本。最后我决定在我的生产环境中安装 Xdebug,我发现一些结果很有趣:

  1. 使用 MySQL 慢查询它不会在服务器上给我带来更好的工作。所以这意味着我的查询一点也不慢。也许这是因为我还没有太多数据。
  2. 安装 memcached 减少了我的一些 PHP 执行时间,有时从 5 分钟缩短到 1 或 2 分钟,但出现了一些问题。 Memcached 不会为我的用户返回新条目。显然,我是在数据库大约 1 分钟后从内存中获取数据。所以无论如何我决定暂时不实现 memcached。与这样的人一起工作的用户并不多。无论如何,时间的速度过得很慢。

这就是我的服务器应用程序的风景。但几天前,出于安全原因,我实现了一个新模块,为我的应用程序上的所有对象创建一个加密密钥。我为此实现了 datacrypt 类,显然它在我的笔记本电脑上的开发环境中完美运行。

我为什么来寻求帮助? 使用 winCachegrind 查看 Xdebug 分析,我发现 datacrypt 的实例化执行需要 37,218 毫秒,我的一些文件显示 10 或 15 行,并且每一行都会动态生成和加密密钥。

datacrypt 类的高使用是一个问题。它用于每个脚本文件中。因此,我需要获得一个新的快速类来创建加密哈希以在我的应用程序中使用。

有人可以给我推荐一个吗?或者我可以做些什么来让 datacrypt 运行得更快?

如果您想看一下我的脚本,请告诉我。

    <div class="property-holder">
<?php

    require BASE_DIR.'/controller/listing.php';

    if(is_array($rows)){
        echo $nav;  
    ?>
<div id="properties-listing" class="content center container-wraps">
<?php
        //$crypt =  new datacrypt();
        foreach($rows as $row){

                //$pkey = $crypt->encrypt($row['property_type'].'-'.$row['property_id'].'-'.$row['broker_id'].'-'.$row['company_id']);
                    if($row[property_type] == 'development'){
                    $picture = (( $row['dev_logo']!='')?'<img class="ui-action" rel="open"  src="'.HOST.'thumbs/80x80/dev/'.$row['dev_logo'].'" />':'<img src="'.HOST.'imgss/noimage.jpg" width="80" height="80" />');
                    }else{
                    $picture = (( $row['image']!='')?'<img class="ui-action" rel="open"  src="'.HOST.'thumbs/80x80/'.$row['property_id'].'/'.$row['image'].'" width="80" height="80" />':'<img src="'.HOST.'imgss/noimage.jpg" width="80" height="80" />');
                    }
                    $prop.=' <div class="listing clearfix shadowbox" id="'.$pkey.'" ref="MLS-'.$row['property_id'].'"  >';
                    $prop.='<div class="listing-photo  left"><a href="#property-view" rel="load-content">'.$picture.'</a></div>';
                    $prop.='<div class="left listing-holder clearfix">';                    
                        $prop.='<h2 class="title"><span class="location"> '.$row['city'].', '.$row['estate'].', '.$row['country'].' </span> '.$row['property_name'].' <em class="company"> ( MLS-'.$row['property_id'].' )</em> '.($_SESSION[company_id]==$row[company_id]?'<span class="company ui-icon ui-icon-heart" ></span>':'').'</h2>';
                        $prop.='<div class="listing-description clearfix"><ul>';

                        $prop.='<div class="threebox left">';
                        $prop.='<li><span>'.T_('Tipo de propiedad').'</span><strong>'.$row['category'].' </strong></li>';   
                        $prop.='<li><span>'.T_('unidad/es disponible').'</span>'.$row['units'].'</li>'; 
                        $prop.='</div>';
                        $prop.='<div class="threebox left">';
                        if((boolean)$row['negotiation']){
                        $prop.='<li>'.T_('precio disponible a').'<strong> '.T_('negociaci&oacute;n').'</strong></li>';  
                        }

                        if((boolean)$row['for_rent'] ){
                                if($row['property_type'] == 'development'){
                                $prop.='<li><span><strong>'.T_('en renta desde:').'</strong></span> $ '.number_format($row['min_rent']).' '.T_('hasta').' '.$row['max_rent'].'</li>';   
                                }else{
                                $prop.='<li><span><strong>'.T_('renta:').'</strong></span> $ '.number_format($row['rent_price']).' '.$row['currency_type'].'</li>'; 
                                }

                            }
                            if((boolean)$row['for_sale'] ){
                                if($row['property_type'] == 'development'){
                                $prop.='<li><span><strong>'.T_('en venta desde:').'</strong></span> $ '.number_format($row['min_sale']).' '.T_('hasta').' '.$row['max_sale'].'</li>';   
                                }else{
                                $prop.='<li><span><strong>'.T_('venta:').'</strong></span>  $ '.number_format($row['sale_price']).' '.$row['currency_type'].'</li>';    
                                }
                            }
                            if((boolean)$row['for_vacational'] ){
                            $prop.='<li><span><strong>'.T_('renta vacacional:').'</strong></span> $ '.number_format($row['min_price']).'  / $ '.number_format($row['mid_price']).' / $ '.number_format($row['hig_price']).' '.$row['currency_type'].'</li>';    
                            }
                            $prop.='<li><span>'.T_('Tipo de propiedad').'</span><strong>'.($row[property_type]=='development'?T_('desarrollo'):$row['category']).' </strong></li>'; 
                            if($row[property_type]=='inventory'){

                            $prop.='<li><strong>'.T_('Pertenece a desarrollo').'</strong></li>';    
                            }

                        $prop.='</div>';
                        $prop.='<div class="threebox left">';
                        $prop.='<li><span>'.T_('Ba&ntilde;os:').'</span><strong> '.$row['bathrooms'].'</strong></li>';  
                        $prop.='<li><span>'.T_('Habitaciones:').'</span> '.$row['bedrooms'].'</li>';    
                        $prop.='</div>';
                        $prop.='</ul></div>';
                    $prop.='</div>';
                    $prop.='<div class="options" >
                            <ul>';
                    $prop.='<li><a rel="open" href="'.HOST.LANG.'/property/?k='.urlencode($pkey).'" title="'.T_('Ver descripcion completa de la propiedad.').'" ><span class="ui-button-icon-primary ui-icon ui-icon-contact"></span>'.T_('ver m&aacute;s').'</a></li>';
                    $prop.='<li><a rel="note" href="#" title="'.T_('Agregar nota a esta propiedad, este mensaje es solo entre la empresa que lista la propiedad y tu como broker.').'" ><span class="ui-button-icon-primary ui-icon ui-icon-tag"></span>'.T_('nota').'</a></li>';
                    $prop.='<li><a rel="info" href="#" title="'.T_('Ver informacion de la empresa que lista esta propiedad.').'" ><span class="ui-button-icon-primary ui-icon ui-icon-contact"></span>'.T_('info').'</a></li>';
                    $prop.='<li><a rel="open" href="'.HOST.LANG.'/pdf/?k='.urlencode($pkey).'" title="'.T_('Descargar presentaci&oacute;n en pdf').'"  title="'.T_('Descargar presentaci&oacute;n en pdf de esta propiedad').'"><span class="ui-button-icon-primary ui-icon ui-icon-circle-arrow-s"></span>'.T_('PDF').'</a></li>';
                    $prop.='</ul>
                        </div>';
                        /*
                    $prop.='<div class="metadata threebox center shadowbox content darknessbox" >';
                        $prop.='<h3>'.$row[companyname].'</h3>';
                        $prop.='<p>'.$row[companyaddress].' </p>';
                        $prop.=' < '.$row[companyweb].' >  '.$row[companyphone].' </p>';
                        $prop.='<p><strong>'.$row[brokername].'</strong> < '.$row[brokeremail].' >  <br />'.$row[brokerphone].'  &mdash; '.$row[brokermobile].'</p>';
                        $prop.='<em>MLS-'.$row['id'].' </em>: <strong>c. '.$row[commission].'</strong>';
                    $prop.=' </div>'; */
                $prop.=' </div>';
            }
            echo $prop;
    ?>
        </div>

    <?php
        echo $nav;  
    }else{
        message(T_('Wow!, lo sentimos no hay propiedades para mostrar'),'warning');
    }
?>
</div>

For more than a week I have been trying to optimize my scripts for MySQL queries. Finally I decided to install Xdebug on my production environment and I found some of the results funny:

  1. Using the MySQL slow query it doesn't return me a higher work on the server. So it means my queries are not slow at all. Perhaps this is because I don't have many data as yet.
  2. Installing the memcached reduced some of my PHP execution time, from 5 mins to 1 or 2 mins sometimes, but some issues came on. Memcached does not return a new entry for my users. Obviously I'm taking datas from memory about 1 minute later of database. So anyway I decide I wouldn't implement memcached for now. There are not many users to work with some like that. Anyway time speed goes slowly.

That's the scenery for my server app. But a few days ago I implemented a new module for security reasons, creating a encrypted key for all my objects on my app. I implemented the datacrypt class for that, it works perfectly on my development enviroment on my laptop obviously.

Why did I came to ask for help?
Looking into the Xdebug profiling using winCachegrind I found that the instantiation of datacrypt takes 37,218ms for execution, some of my files show 10 or 15 rows and each of those rows makes and encrypted key on the fly.

The high use of datacrypt class is a problem. It is used in every script file. So I need to get a new fast class to make an encrypted hash to use in my app.

Can anyone could recommend me one? Or what can I do to make datacrypt run faster?

If you want to take a look at my scripts let me know.

    <div class="property-holder">
<?php

    require BASE_DIR.'/controller/listing.php';

    if(is_array($rows)){
        echo $nav;  
    ?>
<div id="properties-listing" class="content center container-wraps">
<?php
        //$crypt =  new datacrypt();
        foreach($rows as $row){

                //$pkey = $crypt->encrypt($row['property_type'].'-'.$row['property_id'].'-'.$row['broker_id'].'-'.$row['company_id']);
                    if($row[property_type] == 'development'){
                    $picture = (( $row['dev_logo']!='')?'<img class="ui-action" rel="open"  src="'.HOST.'thumbs/80x80/dev/'.$row['dev_logo'].'" />':'<img src="'.HOST.'imgss/noimage.jpg" width="80" height="80" />');
                    }else{
                    $picture = (( $row['image']!='')?'<img class="ui-action" rel="open"  src="'.HOST.'thumbs/80x80/'.$row['property_id'].'/'.$row['image'].'" width="80" height="80" />':'<img src="'.HOST.'imgss/noimage.jpg" width="80" height="80" />');
                    }
                    $prop.=' <div class="listing clearfix shadowbox" id="'.$pkey.'" ref="MLS-'.$row['property_id'].'"  >';
                    $prop.='<div class="listing-photo  left"><a href="#property-view" rel="load-content">'.$picture.'</a></div>';
                    $prop.='<div class="left listing-holder clearfix">';                    
                        $prop.='<h2 class="title"><span class="location"> '.$row['city'].', '.$row['estate'].', '.$row['country'].' </span> '.$row['property_name'].' <em class="company"> ( MLS-'.$row['property_id'].' )</em> '.($_SESSION[company_id]==$row[company_id]?'<span class="company ui-icon ui-icon-heart" ></span>':'').'</h2>';
                        $prop.='<div class="listing-description clearfix"><ul>';

                        $prop.='<div class="threebox left">';
                        $prop.='<li><span>'.T_('Tipo de propiedad').'</span><strong>'.$row['category'].' </strong></li>';   
                        $prop.='<li><span>'.T_('unidad/es disponible').'</span>'.$row['units'].'</li>'; 
                        $prop.='</div>';
                        $prop.='<div class="threebox left">';
                        if((boolean)$row['negotiation']){
                        $prop.='<li>'.T_('precio disponible a').'<strong> '.T_('negociación').'</strong></li>';  
                        }

                        if((boolean)$row['for_rent'] ){
                                if($row['property_type'] == 'development'){
                                $prop.='<li><span><strong>'.T_('en renta desde:').'</strong></span> $ '.number_format($row['min_rent']).' '.T_('hasta').' '.$row['max_rent'].'</li>';   
                                }else{
                                $prop.='<li><span><strong>'.T_('renta:').'</strong></span> $ '.number_format($row['rent_price']).' '.$row['currency_type'].'</li>'; 
                                }

                            }
                            if((boolean)$row['for_sale'] ){
                                if($row['property_type'] == 'development'){
                                $prop.='<li><span><strong>'.T_('en venta desde:').'</strong></span> $ '.number_format($row['min_sale']).' '.T_('hasta').' '.$row['max_sale'].'</li>';   
                                }else{
                                $prop.='<li><span><strong>'.T_('venta:').'</strong></span>  $ '.number_format($row['sale_price']).' '.$row['currency_type'].'</li>';    
                                }
                            }
                            if((boolean)$row['for_vacational'] ){
                            $prop.='<li><span><strong>'.T_('renta vacacional:').'</strong></span> $ '.number_format($row['min_price']).'  / $ '.number_format($row['mid_price']).' / $ '.number_format($row['hig_price']).' '.$row['currency_type'].'</li>';    
                            }
                            $prop.='<li><span>'.T_('Tipo de propiedad').'</span><strong>'.($row[property_type]=='development'?T_('desarrollo'):$row['category']).' </strong></li>'; 
                            if($row[property_type]=='inventory'){

                            $prop.='<li><strong>'.T_('Pertenece a desarrollo').'</strong></li>';    
                            }

                        $prop.='</div>';
                        $prop.='<div class="threebox left">';
                        $prop.='<li><span>'.T_('Baños:').'</span><strong> '.$row['bathrooms'].'</strong></li>';  
                        $prop.='<li><span>'.T_('Habitaciones:').'</span> '.$row['bedrooms'].'</li>';    
                        $prop.='</div>';
                        $prop.='</ul></div>';
                    $prop.='</div>';
                    $prop.='<div class="options" >
                            <ul>';
                    $prop.='<li><a rel="open" href="'.HOST.LANG.'/property/?k='.urlencode($pkey).'" title="'.T_('Ver descripcion completa de la propiedad.').'" ><span class="ui-button-icon-primary ui-icon ui-icon-contact"></span>'.T_('ver más').'</a></li>';
                    $prop.='<li><a rel="note" href="#" title="'.T_('Agregar nota a esta propiedad, este mensaje es solo entre la empresa que lista la propiedad y tu como broker.').'" ><span class="ui-button-icon-primary ui-icon ui-icon-tag"></span>'.T_('nota').'</a></li>';
                    $prop.='<li><a rel="info" href="#" title="'.T_('Ver informacion de la empresa que lista esta propiedad.').'" ><span class="ui-button-icon-primary ui-icon ui-icon-contact"></span>'.T_('info').'</a></li>';
                    $prop.='<li><a rel="open" href="'.HOST.LANG.'/pdf/?k='.urlencode($pkey).'" title="'.T_('Descargar presentación en pdf').'"  title="'.T_('Descargar presentación en pdf de esta propiedad').'"><span class="ui-button-icon-primary ui-icon ui-icon-circle-arrow-s"></span>'.T_('PDF').'</a></li>';
                    $prop.='</ul>
                        </div>';
                        /*
                    $prop.='<div class="metadata threebox center shadowbox content darknessbox" >';
                        $prop.='<h3>'.$row[companyname].'</h3>';
                        $prop.='<p>'.$row[companyaddress].' </p>';
                        $prop.=' < '.$row[companyweb].' >  '.$row[companyphone].' </p>';
                        $prop.='<p><strong>'.$row[brokername].'</strong> < '.$row[brokeremail].' >  <br />'.$row[brokerphone].'  — '.$row[brokermobile].'</p>';
                        $prop.='<em>MLS-'.$row['id'].' </em>: <strong>c. '.$row[commission].'</strong>';
                    $prop.=' </div>'; */
                $prop.=' </div>';
            }
            echo $prop;
    ?>
        </div>

    <?php
        echo $nav;  
    }else{
        message(T_('Wow!, lo sentimos no hay propiedades para mostrar'),'warning');
    }
?>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-10-24 07:41:25

提示: 1- 使用模板将 PHP 代码与 HTML 代码分开。
2- print time() 生成执行前和执行后的时间。

示例:

  <?

  $start=time();
  some code here 1 ,,,,,,,,
  $after1=time();
  $total1=$after1-$start;
  echo "total time for execution this part:  ".$total1

 ?>

tips: 1- use template to separate the PHP code from the HTML code.
2- print time() to generate the time before execution and after the execution.

example:

  <?

  $start=time();
  some code here 1 ,,,,,,,,
  $after1=time();
  $total1=$after1-$start;
  echo "total time for execution this part:  ".$total1

 ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文