首页 新闻 会员 周边

new一个类要1秒多钟,几个意思?大神解释一下一般是什么问题

0
悬赏园豆:10 [已关闭问题] 解决于 2016-01-06 15:09
1 $stime=microtime(true); //测试程序执行时间
2  
3 $db = new login_admin();//数据库操作类
4     
5 $etime=microtime(true);//获取程序执行结束的时间
6 $total=$etime-$stime;   //计算差值
7 echo "<br />[页面执行时间:{$total} ]秒";    

获取时间的位置我做了适当调整,发现这里最花时间,1秒多,

[页面执行时间:1.020828962326 ]秒

把设置时间的这行

$stime=microtime(true);
移到,实例化类的后面,再把结束时间移到代码的最后
$db = new login_admin();
得到的时间是0.0006左右,不知道正常不正常,总之这个实例化真的有问题,不知道几个意思
,大伙帮忙看看
  1 <?php
  2 header('Content-Type:text/html; charset=utf-8;');
  3 class login_admin extends mysql {
  4 
  5     /**
  6      * 用户权限判断($uid, $shell, $m_id)
  7      */
  8     public function Get_user_shell($uid, $shell) {
  9         $query=$this->select(MYSQL_PREFIX_TABLE . 'admin','*','username =\''.$uid.'\''); 
 10         $us = is_array($row = $this->fetch_array($query));
 11         $shell = $us ? $shell == md5($row['username'] . $row['password'] . COOKIERED) : FALSE;
 12         return $shell ? $row : NULL;
 13     } //end shell
 14 
 15     public function Get_user_shell_check($uid, $shell, $m_id = '|0|') {
 16         if ($row=$this->Get_user_shell($uid, $shell)) {
 17             if (strstr($row['m_id'], $m_id)) {
 18                 return $row;
 19             } else {
 20                 echo "你无权限操作!";
 21                 exit ();
 22             } //end m_id
 23         } else {
 24 
 25             return false;
 26           //$this->Get_admin_msg('index.php','请先登陆');
 27         }
 28     } //end shell
 29     //========================================
 30 
 31 
 32     /*
 33      * 用户登陆超时时间(秒)  默认30分钟
 34      * 未超时代表用户活跃,更新活跃时间
 35     */
 36     public function Get_user_ontime($long = '1800') {
 37         if(!isset($_SESSION)){  
 38            session_start();  
 39         }
 40         if (defined('ADMIN_SESSION_TIMEOUT')){
 41             $long=ADMIN_SESSION_TIMEOUT;
 42             }
 43         $new_time = mktime();
 44         $onlinetime = (isset($_SESSION['ontime']) && $_SESSION['ontime']!='')?$_SESSION['ontime'] : 0;
 45 
 46         if ($new_time - $onlinetime > $long) {
 47             session_destroy();
 48             //echo "登录超时";
 49             //exit ();
 50             return false;
 51         } else {
 52             $_SESSION['ontime'] = mktime();
 53             return true;
 54         }
 55     }
 56 
 57     /**
 58      * 用户登陆
 59      */
 60     public function Get_user_login($username, $password) {
 61         if(isset($_POST["yzm"]) && $_POST["yzm"]==$_SESSION['ValidateCode_session']){
 62             $username = str_replace(" ", "", $username);
 63             $query=$this->select(MYSQL_PREFIX_TABLE . 'admin','*','username=\''.$username.'\'');  
 64             $us = is_array($row = $this->fetch_array($query));
 65             ;
 66             $ps = $us ? md5($password) == $row['password'] : FALSE;
 67             if ($ps) {
 68                 $_SESSION['uid'] = $row['username'];
 69                 $_SESSION['shell'] = md5($row['username'] . $row['password'] . COOKIERED);
 70                 $_SESSION['ontime'] = mktime();
 71                 //提示框
 72                 include_once DOCUMENT_ROOT . "/class/msg.fun.php";
 73                 $msg=new msg();
 74                 $msgarray=array(
 75                         'msg'=>array('title'=>'提示信息', 
 76                                     'content'=>'用户登陆成功!', 
 77                                     'link'=>'admin.php',
 78                                     'markimg'=>'ok')
 79                         );
 80                 $msg->display($msgarray, 'msg_ok');
 81                 //$this->Get_admin_msg('admin.php','登陆成功!');
 82             } else {
 83                 //提示框
 84                 include_once DOCUMENT_ROOT . "/class/msg.fun.php";
 85                 $msg=new msg();
 86                 $msgarray=array(
 87                         'msg'=>array('title'=>'提示信息', 
 88                                     'content'=>'密码或用户错误!', 
 89                                     'link'=>'index.php',
 90                                     'markimg'=>'no')
 91                         );
 92                 $msg->display($msgarray, 'msg_ok');
 93                 //$this->Get_admin_msg('index.php','密码或用户错误!');
 94                 session_destroy();
 95             }
 96         }else{
 97                 //提示框
 98                 include_once DOCUMENT_ROOT . "/class/msg.fun.php";
 99                 $msg=new msg();
100                 $msgarray=array(
101                         'msg'=>array('title'=>'提示信息', 
102                                     'content'=>'验证码输入错误!', 
103                                     'link'=>'index.php',
104                                     'markimg'=>'no')
105                         );
106                 $msg->display($msgarray, 'msg_ok');
107                 //$this->Get_admin_msg('index.php','验证码输入错误!');
108                 session_destroy();
109         }
110     }
111      /**
112       * 用户退出登陆
113       */
114     public function Get_user_out() {
115         session_destroy();
116         //提示框
117         include_once DOCUMENT_ROOT . "/class/msg.fun.php";
118                 $msg=new msg();
119         $msgarray=array(
120                 'msg'=>array('title'=>'提示信息', 
121                             'content'=>'退出成功!', 
122                             'link'=>'index.php',
123                             'markimg'=>'ok')
124                 );
125         $msg->display($msgarray, 'msg_ok');
126         //$this->Get_admin_msg('index.php','退出成功!');
127     }
128 
129     //========================
130 } //end class

 

为什么会这样,只是创建实例而已,至于吗?还没执行算法呢.

问题补充:
1     /*重载构造函数*/
2     public function __construct(){
3         $a = func_get_args();     //获取构造函数中的参数
4         $i = count($a);            //按参数个数查找函数方法: (__construct . $i)
5         if (method_exists($this,$f='__construct'.$i)) {        //验证函数是否存在
6         call_user_func_array(array($this,$f),$a);
7             }
8         }

构造函数是这样,,实例化的时候并没有参数

故,只是赋值操作

 1     //0个参数的构造函数__construct0
 2     public function __construct0() {
 3         $this->db_host = MYSQL_HOST;
 4         $this->db_user = MYSQL_USER;
 5         $this->db_pwd = MYSQL_PWD;
 6         $this->db_database = MYSQL_DATABASE;
 7         $this->conn = MYSQL_CONN;
 8         $this->coding = MYSQL_CODING;
 9         $this->connect();
10     }
女子100的主页 女子100 | 初学一级 | 园豆:85
提问于:2016-01-04 19:18
< >
分享
其他回答(1)
0

肯定不是new对象花费那么长时间 看看是不是 本身 mysql 连接的时候 耗费的时间长, 也就是主要因为网络的原因。 没猜错的话你的数据库应该是远程的数据库。

而在你的mysql 类里面 有初始的时候进行 mysql 连接的代码

收获园豆:2
Acmen | 园豆:173 (初学一级) | 2016-01-04 22:13

我数据库是本地的,网站和数据库都在本地,还没发布呢

支持(0) 反对(0) 女子100 | 园豆:85 (初学一级) | 2016-01-05 08:29
0

打日志记录一些每个可能耗时的操作的,操作时间吧,定位到耗时的操作

收获园豆:2
jackccc | 园豆:1341 (小虾三级) | 2016-01-05 15:26
if ($this->conn == 'pconn') { //永久链接
$this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd); } else {
//即使链接
$this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd); }

我查到这个问题,用mysql_connect这个链接数据库会有1秒延迟,反之没有,,

没有深入学习过,不知道这两个会有什么优缺点

支持(0) 反对(0) 女子100 | 园豆:85 (初学一级) | 2016-01-06 09:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册