当脚本在连续循环中加载单个库时会发生什么?
What happens when a script loads a single library in a continuous loop?
我学习编程已经有一段时间了。 PHP、Javascript 等等,我还不是专家。但我知道插件和库,它们的作用和使用方式。
现在我知道一个库一旦加载就可以在代码中重复使用。但是如果我的代码结构要求我在一个连续的循环中加载一个库,比如说 100,000 次。
如果有的话,这会对托管我的应用程序的服务器产生什么影响?
特别是在内存和进程消耗等方面......
例如。使用 codeigniter 发送电子邮件需要;
function send_mail(){
$this->load->library('email');
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
}
如果我需要为批量电子邮件服务调用此函数 100,000 次怎么办?
您的代码对内存没有影响。 CodeIgniter 中的库 (classes) 是 "singletons",这意味着一旦库被加载,任何额外的加载相同库的尝试都将简单地 return 第一次创建的实例 load
被称为。换句话说,加载的任何库都只会创建一个实例。
对处理有一些影响,因为对 $this->load->library('email');
的调用将 运行 代码确定 email
已经加载。代码不多,但它在一个循环中加起来,完全没有必要。
假设 function send_mail(){
是 class(例如控制器)的一部分,那么您可以通过在库的构造函数中加载 email
库来消除额外的处理。
这是一个示例控制器。
class App_emailer extends CI_Controller
{
public function __construct()
{
parent :: __construct();
$this->load->library('email');
}
public function send_mail()
{
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
}
}
1.Load 构造中的库 class
要么
2.Autoload config/autoload 中的库。php:行
$autoload['libraries']=array('email');
我学习编程已经有一段时间了。 PHP、Javascript 等等,我还不是专家。但我知道插件和库,它们的作用和使用方式。
现在我知道一个库一旦加载就可以在代码中重复使用。但是如果我的代码结构要求我在一个连续的循环中加载一个库,比如说 100,000 次。
如果有的话,这会对托管我的应用程序的服务器产生什么影响?
特别是在内存和进程消耗等方面......
例如。使用 codeigniter 发送电子邮件需要;
function send_mail(){
$this->load->library('email');
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
}
如果我需要为批量电子邮件服务调用此函数 100,000 次怎么办?
您的代码对内存没有影响。 CodeIgniter 中的库 (classes) 是 "singletons",这意味着一旦库被加载,任何额外的加载相同库的尝试都将简单地 return 第一次创建的实例 load
被称为。换句话说,加载的任何库都只会创建一个实例。
对处理有一些影响,因为对 $this->load->library('email');
的调用将 运行 代码确定 email
已经加载。代码不多,但它在一个循环中加起来,完全没有必要。
假设 function send_mail(){
是 class(例如控制器)的一部分,那么您可以通过在库的构造函数中加载 email
库来消除额外的处理。
这是一个示例控制器。
class App_emailer extends CI_Controller
{
public function __construct()
{
parent :: __construct();
$this->load->library('email');
}
public function send_mail()
{
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
}
}
1.Load 构造中的库 class 要么 2.Autoload config/autoload 中的库。php:行
$autoload['libraries']=array('email');