<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>阅微堂 &#187; 优化</title>
	<atom:link href="http://zhiqiang.org/blog/tag/%e4%bc%98%e5%8c%96/feed" rel="self" type="application/rss+xml" />
	<link>http://zhiqiang.org/blog</link>
	<description>理工科背景的证券从业人员</description>
	<lastBuildDate>Sun, 05 Feb 2012 03:59:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>整数规划思想求解数独游戏</title>
		<link>http://zhiqiang.org/blog/it/integer-programming-solving-sudoku.html</link>
		<comments>http://zhiqiang.org/blog/it/integer-programming-solving-sudoku.html#comments</comments>
		<pubDate>Sat, 25 Sep 2010 00:56:41 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[matlab]]></category>
		<category><![CDATA[优化]]></category>
		<category><![CDATA[数独]]></category>
		<category><![CDATA[生活中的Matlab]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=1149</guid>
		<description><![CDATA[博客 » IT技术 » matlab » 系列：生活中的Matlab 查看该系列所有文章 最近做一些优化问题，找到了YALMIP工具包。在其帮助文件里看到如何使用该工具包求解sudoku，整个思路是将问题转化为整数规划问题。这样的思路以前也想到过，但总觉得整数规划问题的求解会更复杂。但是下面的Matlab代码，显示它可以非常简洁，思路见程序的注释（程序运行需要安装YALMIP工具包）： % 初始状...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/matlab'>matlab</a>  » </p><div class="series"><span>系列：<b>生活中的Matlab</b></span><br/>
<a href="http://zhiqiang.org/blog/tag/%e7%94%9f%e6%b4%bb%e4%b8%ad%e7%9a%84matlab">查看该系列所有文章</a>
<div id='series'></div>
</div>  <p>最近做一些<a href="http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html">优化问题</a>，找到了YALMIP工具包。在其帮助文件里看到如何使用该工具包<a href="http://users.isy.liu.se/johanl/yalmip/pmwiki.php?n=Examples.Sudoku" target="_blank">求解sudoku</a>，整个思路是将问题转化为整数规划问题。这样的思路以前也想到过，但总觉得整数规划问题的求解会更复杂。但是下面的Matlab代码，显示它可以非常简洁，思路见程序的注释（程序运行需要安装YALMIP工具包）：</p>
<blockquote>
<pre>% 初始状态，0表示没填的格子
S=[ 9 0 0 0 0 0 0 0 5
    0 4 0 3 0 0 0 2 0
    0 0 8 0 0 0 1 0 0
    0 7 0 6 0 3 0 0 0
    0 0 0 0 8 0 0 0 0
    0 0 0 7 0 9 0 6 0
    0 0 1 0 0 0 9 0 0
    0 3 0 0 0 6 0 4 0
    5 0 0 0 0 0 0 0 8];

% 定义0、1数组 A(i, j, k) = 1，如果方格(i, j)里的数为k；否则为0。
% 求解sudoku问题即求一定假设条件下的解。
p = 3;
A = binvar(p^2,p^2,p^2,'full');

% 以下为限制条件
F = [sum(A,1) == 1]; % 限制每行每个数恰好一个
F = [F, sum(A,2) == 1]; % 限制每列每个数恰好一个
F = [F, sum(A,3) == 1]; % 限制每个单元格子里恰好一个数

<span style="color: #0000ff;">for </span>m = 1:p
    <span style="color: #0000ff;">for </span>n = 1:p
        <span style="color: #0000ff;">for </span>k = 1:p^2
            s = sum(sum(A((m-1)*p+(1:p),(n-1)*p+(1:p),k)));
            F = [F, s == 1];  % 限制每个3×3的方框里每个数恰好出现一次
        <span style="color: #0000ff;">end</span>
    <span style="color: #0000ff;">end</span>
<span style="color: #0000ff;">end</span>

<span style="color: #0000ff;">for </span>i = 1:p^2
    <span style="color: #0000ff;">for </span>j = 1:p^2
        <span style="color: #0000ff;">if </span>S(i,j)
            F = [F, A(i,j,S(i,j)) == 1]; % 初始给定的数要一直
        <span style="color: #0000ff;">end</span>
    <span style="color: #0000ff;">end</span>
<span style="color: #0000ff;">end</span>

% 直接求解
sol = solvesdp(F); 

Z = 0;
<span style="color: #0000ff;">for </span>i = 1:p^2
      Z = Z  + i*double(A(:,:,i)); % 简单相加即可
<span style="color: #0000ff;">end</span>
Z % 输出结果</pre>
</blockquote>
<p>或者直接下载源代码文件：</p>
Note: There is a file embedded within this post, please visit this post to download the file.
<p>程序中的例子S是我在网上搜“最难 数独”找到的一个例子，程序在几秒钟内便能找出答案。</p>
<p>我以前有段时间特别喜欢玩数独，曾经把PSP上的一个数独游戏玩穿（大概有150关）。现在发现，<strong>人所谓的那点逻辑推理能力，在强大的计算能力前面不堪一击。</strong></p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/batch-resize-images-using-matlab.html">批量修改图片大小的Matlab脚本</a></li><li><a href="http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html">Matlab的优化工具箱</a></li><li><a href="http://zhiqiang.org/blog/it/sync-and-backup-using-matlab.html">用Matlab脚本同步和备份资料</a></li><li><a href="http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html">加快WordPress的页面生成和载入速度</a></li><li><a href="http://zhiqiang.org/blog/it/blog-fast-download-speeds-to-be-faster-and-the-speed-of-blog.html">blog下载速度多快才算快和blog速度优化</a></li><li><a href="http://zhiqiang.org/blog/it/date-series-in-matlab-excel-sql.html">Matlab、Excel、SQL中的日期的数字序列形式</a></li><li><a href="http://zhiqiang.org/blog/it/parallel-computing-in-matlab.html">Matlab的并行计算</a></li><li><a href="http://zhiqiang.org/blog/it/save-load-data-v6-is-fast.html">尽量使用Matlab的save -v6</a></li><li><a href="http://zhiqiang.org/blog/it/how-many-codes-have-you-written.html">统计Matlab代码量</a></li><li><a href="http://zhiqiang.org/blog/it/fix-bug-in-runstoredprocedure.html">runstoredprocedure有个小bug</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2010. | <a href="http://zhiqiang.org/blog/it/integer-programming-solving-sudoku.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/integer-programming-solving-sudoku.html#comments">12 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/integer-programming-solving-sudoku.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Matlab的优化工具箱</title>
		<link>http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html</link>
		<comments>http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html#comments</comments>
		<pubDate>Thu, 23 Sep 2010 14:13:56 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[matlab]]></category>
		<category><![CDATA[优化]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=1135</guid>
		<description><![CDATA[博客 » IT技术 » matlab » 最近做了些东西，用到了Matlab的优化工具箱，optimization toolbox。因为以前没用过这东西，今天把这个工具箱的帮助文档基本上翻了一遍。 这个工具箱的确非常之强大，如它所说，所有的优化问题都能解，包括自定义的目标函数，甚至自定义的限制条件。最后解出来是什么样子就不确定了。 以下是Matlab推荐的各种优化问题对应的Solver。 限制条件 优化目...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/matlab'>matlab</a>  » </p><p>最近做了些东西，用到了Matlab的优化工具箱，optimization toolbox。因为以前没用过这东西，今天把这个工具箱的帮助文档基本上翻了一遍。</p>
<p>这个工具箱的确非常之强大，如它所说，所有的优化问题都能解，包括自定义的目标函数，甚至自定义的限制条件。最后解出来是什么样子就不确定了。</p>
<p>以下是Matlab推荐的各种优化问题对应的Solver。</p>
<table border="1" cellspacing="0" cellpadding="0" width="89%">
<thead>
<tr>
<td rowspan="2" width="17%" valign="top">
<p style="text-align: center;"><strong>限制条件</strong></p>
</td>
<td colspan="5" width="82%" valign="top">
<p style="text-align: center;"><strong>优化目标</strong></p>
</td>
</tr>
<tr>
<td width="14%" valign="top"><strong>线性</strong></td>
<td width="14%" valign="top"><strong>二次（正定）</strong></td>
<td width="17%" valign="top"><strong>自定义函数</strong></td>
<td width="17%" valign="top"><strong>不光滑</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td width="17%" valign="top">
<p style="text-align: center;">无</p>
</td>
<td width="14%" valign="top"></td>
<td width="14%" valign="top">quadprog</td>
<td width="17%" valign="top">fminsearch<br />
fminunc</td>
<td width="17%" valign="top">fminsearch<br />
*</td>
</tr>
<tr>
<td width="17%" valign="top">
<p style="text-align: center;">线性</p>
</td>
<td width="14%" valign="top">linprog</td>
<td width="14%" valign="top">quadprog</td>
<td width="17%" valign="top">fmincon<br />
fseminf</td>
<td width="17%" valign="top">*</td>
</tr>
<tr>
<td width="17%" valign="top">
<p style="text-align: center;">自定义函数</p>
</td>
<td width="14%" valign="top">fmincon</td>
<td width="14%" valign="top">fmincon</td>
<td width="17%" valign="top">fmincon<br />
fseminf</td>
<td width="17%" valign="top">*</td>
</tr>
<tr>
<td width="17%" valign="top">
<p style="text-align: center;">整数</p>
</td>
<td width="14%" valign="top">bintprog</td>
<td width="14%" valign="top"></td>
<td width="17%" valign="top"></td>
<td style="text-align: center;" width="17%" valign="top"></td>
</tr>
</tbody>
</table>
<p>Matlab的帮助文档里用了很小一个片段说明了，以上所有的规划问题，都只是寻找局部解。不过如果优化函数是凸函数，那么局部最优（最小）解就是全局最优解。而线性、二次函数都是凸的，所以对线性和二次目标函数而言找的都是全局最优解。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/integer-programming-solving-sudoku.html">整数规划思想求解数独游戏</a></li><li><a href="http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html">加快WordPress的页面生成和载入速度</a></li><li><a href="http://zhiqiang.org/blog/it/blog-fast-download-speeds-to-be-faster-and-the-speed-of-blog.html">blog下载速度多快才算快和blog速度优化</a></li><li><a href="http://zhiqiang.org/blog/it/class-wrapper-functions-in-matlab.html">Matlab中用类封装函数</a></li><li><a href="http://zhiqiang.org/blog/it/batch-resize-images-using-matlab.html">批量修改图片大小的Matlab脚本</a></li><li><a href="http://zhiqiang.org/blog/it/use-adodb-in-matlab.html">Matlab中使用ADODB访问数据库</a></li><li><a href="http://zhiqiang.org/blog/it/datestr-datenum-efficiency-in-matlab.html">Matlab中datestr和datenum函数效率问题</a></li><li><a href="http://zhiqiang.org/blog/it/parallel-computing-in-matlab.html">Matlab的并行计算</a></li><li><a href="http://zhiqiang.org/blog/resource/matlab-2011a-released.html">Matlab 2011a</a></li><li><a href="http://zhiqiang.org/blog/it/config-64bit-matlab-compiler.html">配置64位Matlab的编译器</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2010. | <a href="http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html#comments">5 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>blog下载速度多快才算快和blog速度优化</title>
		<link>http://zhiqiang.org/blog/it/blog-fast-download-speeds-to-be-faster-and-the-speed-of-blog.html</link>
		<comments>http://zhiqiang.org/blog/it/blog-fast-download-speeds-to-be-faster-and-the-speed-of-blog.html#comments</comments>
		<pubDate>Sat, 02 Sep 2006 04:39:09 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[优化]]></category>
		<category><![CDATA[速度]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/429.html</guid>
		<description><![CDATA[博客 » IT技术 » WordPress » 很多人有一个大致的感觉，某blog很慢，或者有点慢，但是似乎还过得去，而没有一个量化的指标。其实对于FireFox浏览器，使fastfox插件，可以在右下方显示页面的载入时间。注意测试时将fastfox插件选项调为默认，既不进行任何优化，因为我们必须假设大多数用户没有安装此插件。 对于一个网页的显示速度，有两个方面可以衡量，一个是内容首现速度...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/wordpress'>WordPress</a>  » </p><p>很多人有一个大致的感觉，某blog很慢，或者有点慢，但是似乎还过得去，而没有一个量化的指标。其实对于FireFox浏览器，使<a rel="fastfox插件安装地址" target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/1269/">fastfox</a>插件，可以在右下方显示页面的载入时间。注意测试时将fastfox插件选项调为默认，既不进行任何优化，因为我们必须假设大多数用户没有安装此插件。</p>
<p>对于一个网页的显示速度，有两个方面可以衡量，一个是内容首现速度，另一个是页面完成速度。比如说臃肿的<a href="http://news.sina.com.cn/">sina新闻</a>首页，2秒就可以开始浏览新闻了，但是20秒之后才会把页面显示完全。由于游览器一般都是边下载便显示内容，所以我认为前者更为重要一些。</p>
<p>另外，对于访问者而言，显示速度还分为两种，首次访问速度和页面跳转速度。对于一些页面文件和图片比较多的网页，首次访问速度非常慢，但是进去之后在网页之间的跳转就快得多了，因为浏览器会缓存很多文件。</p>
<p>在网上看到一篇文章——<a target="_blank" href="http://www.marketingman.net/wminfo/4130.htm">网站设计参考：网站下载速度多快才算快</a>：</p>
<blockquote><p>网站下载速度的快慢，在不同时期，不同的网络环境条件下，其绝对指标会有很大差别，现在1秒属于下载快的水平，但再过2年之后，说不定用户已经对1秒钟这样长的下载时间无法忍受了。</p>
<p>雅虎网站创始人的杨致远曾说过，为打开一个网页，网民最大的忍受时间为8秒，如果超出了这个时间，用户就会走掉。</p></blockquote>
<p>我自己这个blog在我这里的连接速度大概是首次访问5到7秒钟(通过CTR+F5强制刷新得到)，页面间的跳转则需要大约2到4秒钟，根据所显示的页面长度略有差别。按照上面的标准，已经是非常慢的了。</p>
<p>但是，我现在的网络状况下，我所上的大部分网站的速度都无法达到上面文章里提到的速度。就拿blog而言，我现在访问的很多blog的首次访问速度都超过了上面所说的8秒的临界值。那种等上10秒钟还没有显示一个字的blog是非常恐怖的，我基本上是强制刷新重新载入或者直接关掉。</p>
<p>所以，一些优化是很必要的。在我<a href="http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html">加快WordPress的页面生成和载入速度</a>之前，我的blog也是10秒一族，但现在快了一倍以上，至少在我这里（北京教育网）是这样的。后来看到<a href="http://my.donews.com/htmlor/2006/08/03/serving_javascript_fast_chinese/">flickr对javascript干的好事</a>这篇文章，这里讲了更多的关于php和javascript的优化技巧，其中就有合并js文档和缓存页面等。我想某些站长应该考虑一下其中的方法。</p>
<p>一个更直接，而且不需要任何技术的方法是尽量不用需要下载js和css的插件（比如很多人喜欢安装coolplayer这类播放器插件，但对于一般人来说，根本不需要这么强大的播放器定制功能，一段简单的代码足够用了），以及使用图片较少的模版。把有的站点的源代码当下来一看，光js文件就10多个，这是非常恐怖的。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html">加快WordPress的页面生成和载入速度</a></li><li><a href="http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html">Matlab的优化工具箱</a></li><li><a href="http://zhiqiang.org/blog/it/integer-programming-solving-sudoku.html">整数规划思想求解数独游戏</a></li><li><a href="http://zhiqiang.org/blog/it/all-articles-into-msn-spaces.html">导入MSN Spaces所有文章</a></li><li><a href="http://zhiqiang.org/blog/it/my-experiences-and-views-blog.html">我的Blog经历和一些看法</a></li><li><a href="http://zhiqiang.org/blog/it/to-get-gibberish-wordpress-move-to-dreamhost.html">搞定乱码，WordPress搬家到dreamhost</a></li><li><a href="http://zhiqiang.org/blog/it/wordpress-21-solution-can-not-remove-the-article-upgrade-to-213.html">WordPress 2.1无法删除文章解决方法 &amp; 升级到2.1.3</a></li><li><a href="http://zhiqiang.org/blog/it/bbpress-installation-problems-and-several-plug-ins.html">bbPress的安装问题和几个插件</a></li><li><a href="http://zhiqiang.org/blog/it/wordpress-blog-has-different-domains.html">同一WordPress的blog可拥有多个域名</a></li><li><a href="http://zhiqiang.org/blog/it/how-delete-wordpress-category-base.html">如何去掉category permalink base？</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2006. | <a href="http://zhiqiang.org/blog/it/blog-fast-download-speeds-to-be-faster-and-the-speed-of-blog.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/blog-fast-download-speeds-to-be-faster-and-the-speed-of-blog.html#comments">5 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/blog-fast-download-speeds-to-be-faster-and-the-speed-of-blog.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>加快WordPress的页面生成和载入速度</title>
		<link>http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html</link>
		<comments>http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html#comments</comments>
		<pubDate>Sat, 26 Aug 2006 05:31:06 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP-Cache 2.0]]></category>
		<category><![CDATA[优化]]></category>
		<category><![CDATA[加速]]></category>
		<category><![CDATA[静态化]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/420.html</guid>
		<description><![CDATA[博客 » IT技术 » WordPress » 前一阵，此blog的页面打开速度慢的要命，我每次打开都需要大约10秒钟的时间。而通过Google Analytics的数据看到，70%+用户在这个blog的停留时间还不到十秒:(。所以我决定加速一下我的blog，后来看起来似乎有些效果。下面是我用的一些方法，希望对后来人有些用处。 安装WP-Cache 2.0 WP-Cache 2.0可以将页面缓存，在短时间内(默认为1个小时，可以在后台设置)...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/wordpress'>WordPress</a>  » </p><p>前一阵，此blog的页面打开速度慢的要命，我每次打开都需要大约10秒钟的时间。而通过Google Analytics的数据看到，70%+用户在这个blog的停留时间还不到十秒:(。所以我决定加速一下我的blog，后来看起来似乎有些效果。下面是我用的一些方法，希望对后来人有些用处。</p>
<h3><font size="3">安装</font><a href="http://mnm.uib.es/gallir/wp-cache-2/">WP-Cache 2.0</a></h3>
<p>WP-Cache 2.0可以将页面缓存，在短时间内(默认为1个小时，可以在后台设置)的再次浏览，将直接发送缓存的页面，相当于将WordPress给静态化了。</p>
<p>好处是显然的。而且这个版本的WP-Cache 2.0在缓存的时候根据页面URL+Cookie进行缓存的，所以它不会影响COOKIE的使用。注意，WP-Cache 2.0只读取Cookie中的email地址，然后根据email地址判定是否属于同一个用户。由于大多数人都没有留下Email，所以我修改成根据Cookie中的用户名来判断，将插件目录下的wp-cache-phase1.php的第67行改成：</p>
<blockquote>
<p align="left">//if (preg_match("/^wordpress|^comment_author_email_/", $key)) {<br />
if (preg_match("/^wordpress|^comment_author_/", $key)) {</p>
</blockquote>
<p align="left">由于大部浏览者的cookie都是空的，所以这个插件会加快这大部分的浏览速度。另外，这个插件还会侦测文章更新，实时更新缓存等。</p>
<p align="left">但是缺点也是显然的。首先，在页面还没有生成的时候，就进行COOKIE的读取和交换，将降低速度，某种程度上来说，它使得头次浏览的速度更慢。再者，这个插件在运行的时候，还是得先把其他插件初始化，所以它只是减少了页面生成的函数调用的时间。</p>
<p align="left"><strong>效果：不明显。</strong>事实上，这个插件主要是用来降低系统CPU占用量的，对页面载入速度影响有限。</p>
<h3>改善插件</h3>
<p>我并没有安装太多插件，而且都是些小型的插件，从来不装像UTW那样的巨无霸插件。但是我还是把那些非插件必须的文件(大部分是我修改插件的时候新加的)都移出插件目录。以免WP错把它们当成插件文件进行初始化处理。</p>
<p><strong>效果不明</strong>。因为经过处理的文件不多也不大，想必影响有限。</p>
<h3>页面静态化</h3>
<p>一个不变的事实是：html页面永远要比php页面快的多，而且几乎不占用CPU资源。所以将页面静态化是一个好主意。但将所有页面静态化，没有这个必要。所以我选择了将blog主页进行静态化处理——各位如果不信的话，直接页面刷新，然后看一下右边的随机文章变了没有:)。</p>
<p>我同学告诉我页面静态化有很多种方法，但我决定采用最土的一种方法：</p>
<p>首先，在根目录下建立一个cron.sh文件，里面的内容是：</p>
<blockquote><p>wget -O /home/aaaa/zhiqiang.org/blog/index.html http://zhiqiang.org/blog/index.php</p></blockquote>
<p>上面的/home/aaaa是我的根目录，其中aaaa是用户名。再使用Fterm SSH登录远程主机，输入</p>
<blockquote><p>crontab -e</p></blockquote>
<p>在出来的编辑界面中输入</p>
<blockquote><p>*/10 * * * * /home/aaaa/cron.sh</p></blockquote>
<p>再保存就OK了。这样，系统会自动地每10分钟一次将blog的动态主页index.php转成静态的index.html，以后访问主页的时候就会直接访问index.html，从而实现了blog主页面的html静态化。</p>
<p>缺点：COOKIE失效。不过山人自有妙计，<a target="_blank" href="http://www.leexuan.com/?f">使用javascript本地读取COOKIE</a>然后填入表格即可。</p>
<p><strong>效果：明显</strong>。</p>
<h3><font size="3">合并JS和CSS文件</font></h3>
<p>浏览页面的时候，把页面保存下来，便能看到这个页面含有的文件。文件太多，会大大降低浏览速度。而WordPress的插件系统使得JS和CSS文件很多，所以，应当合并一下JS和CSS文件。</p>
<p>手动合并是一个方法，但是容易弄错，而且不太好修改。另一个方法是建立一个新的all.js.php文件，内容是：</p>
<blockquote><p><span style="color: #0000ff">&lt;?</span>php<br />
require_once('目录/wp-blog-header.php' ) ;<br />
<a style="color: #0000ff" href="http://cn.php.net/manual/en/function.include.php">include</a> ("<span style="color: #8b0000">/blog/a.js</span>" ) ;<br />
<a style="color: #0000ff" href="http://cn.php.net/manual/en/function.include.php">include</a> ("<span style="color: #8b0000">/b.js</span>" ) ;<br />
<span style="color: #0000ff">?&gt;</span></p></blockquote>
<p>就是将header.php里面的那些js文件都include到all.js.php，然后在header里面包含all.js.php文件即可：</p>
<blockquote><p>&lt;script xsrc="<span style="color: #8b0000">all.js.php</span>" type="<span style="color: #8b0000">text/javascript</span>"/&gt;</p></blockquote>
<p>另外还要做的一件事情是需要把原来那些js文件从head里面去掉。通常这些js文件都是通过add_action('wp_head','fun' ) ;这样的钩子添加到head里面的。在fun函数的定义处去掉对应的代码，或者直接把这条语句删除即可。</p>
<p>对CSS文件也可以用同样的处理方式。CSS文件一般都不多，这时候可以直接手动把它们都贴到一起。</p>
<p>另外js文件的载入位置也很重要，放在越后面越好，让浏览器先下载html代码。像Google Analytics的js代码最好放在文件最后。</p>
<p><strong>效果：非常明显</strong>。</p>
<p>另外，太多的图片也会降低载入速度，而我这个模板的图片就比较多，而且我已经把能不用的图片去掉了。想着是不是该换模板了。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/blog-fast-download-speeds-to-be-faster-and-the-speed-of-blog.html">blog下载速度多快才算快和blog速度优化</a></li><li><a href="http://zhiqiang.org/blog/it/a-new-static-wordpress-plug-cos-of-cache-html.html">一个新的WordPress静态化插件cos-html-cache</a></li><li><a href="http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html">Matlab的优化工具箱</a></li><li><a href="http://zhiqiang.org/blog/it/integer-programming-solving-sudoku.html">整数规划思想求解数独游戏</a></li><li><a href="http://zhiqiang.org/blog/it/all-articles-into-msn-spaces.html">导入MSN Spaces所有文章</a></li><li><a href="http://zhiqiang.org/blog/it/my-experiences-and-views-blog.html">我的Blog经历和一些看法</a></li><li><a href="http://zhiqiang.org/blog/it/to-get-gibberish-wordpress-move-to-dreamhost.html">搞定乱码，WordPress搬家到dreamhost</a></li><li><a href="http://zhiqiang.org/blog/it/bbpress-installation-problems-and-several-plug-ins.html">bbPress的安装问题和几个插件</a></li><li><a href="http://zhiqiang.org/blog/it/wordpress-blog-has-different-domains.html">同一WordPress的blog可拥有多个域名</a></li><li><a href="http://zhiqiang.org/blog/it/how-delete-wordpress-category-base.html">如何去掉category permalink base？</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2006. | <a href="http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html#comments">52 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html/feed</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
	</channel>
</rss>

