2010年2月2日 星期二

HipHop for PHP: Move Fast

One of the key values at Facebook is to move fast. For the past six years, we have been able to accomplish a lot thanks to rapid pace of development that PHP offers. As a programming language, PHP is simple. Simple to learn, simple to write, simple to read, and simple to debug. We are able to get new engineers ramped up at Facebook a lot faster with PHP than with other languages, which allows us to innovate faster.


Today I'm excited to share the project a small team of amazing people and I have been working on for the past two years; HipHop for PHP. With HipHop we've reduced the CPU usage on our Web servers on average by about fifty percent, depending on the page. Less CPU means fewer servers, which means less overhead. This project has had a tremendous impact on Facebook. We feel the Web at large can benefit from HipHop, so we are releasing it as open source this evening in hope that it brings a new focus toward scaling large complex websites with PHP. While HipHop has shown us incredible results, it's certainly not complete and you should be comfortable with beta software before trying it out.

HipHop for PHP isn't technically a compiler itself. Rather it is a source code transformer. HipHop programmatically transforms your PHP source code into highly optimized C++ and then uses g++ to compile it. HipHop executes the source code in a semantically equivalent manner and sacrifices some rarely used features — such as eval() — in exchange for improved performance. HipHop includes a code transformer, a reimplementation of PHP's runtime system, and a rewrite of many common PHP Extensions to take advantage of these performance optimizations.

Scaling PHP as a Scripting Language
PHP's roots are those of a scripting language, like Perl, Python, and Ruby, all of which have major benefits in terms of programmer productivity and the ability to iterate quickly on products. This is compared to more traditional compiled languages like C++ and interpreted languages like Java. On the other hand, scripting languages are known to generally be less efficient when it comes to CPU and memory usage. Because of this, it's been challenging to scale Facebook to over 400 billion PHP-based page views every month.

One common way to address these inefficiencies is to rewrite the more complex parts of your PHP application directly in C++ as PHP Extensions. This largely transforms PHP into a glue language between your front end HTML and application logic in C++. From a technical perspective this works well, but drastically reduces the number of engineers who are able to work on your entire application. Learning C++ is only the first step to writing PHP Extensions, the second is understanding the Zend APIs. Given that our engineering team is relatively small — there are over one million users to every engineer — we can't afford to make parts of our codebase less accessible than others.

Scaling Facebook is particularly challenging because almost every page view is a logged-in user with a customized experience. When you view your home page we need to look up all of your friends, query their most relevant updates (from a custom service we've built called Multifeed), filter the results based on your privacy settings, then fill out the stories with comments, photos, likes, and all the rich data that people love about Facebook. All of this in just under a second. HipHop allows us to write the logic that does the final page assembly in PHP and iterate it quickly while relying on custom back-end services in C++, Erlang, Java, or Python to service the News Feed, search, Chat, and other core parts of the site.

Since 2007 we've thought about a few different ways to solve these problems and have even tried implementing a few of them. The common suggestion is to just rewrite Facebook in another language, but given the complexity and speed of development of the site this would take some time to accomplish. We've rewritten aspects of the Zend Engine — PHP's internals — and contributed those patches back into the PHP project, but ultimately haven't seen the sort of performance increases that are needed. HipHop's benefits are nearly transparent to our development speed.

Hacking Up HipHop
One night at a Hackathon a few years ago (see Prime Time Hack), I started my first piece of code transforming PHP into C++. The languages are fairly similar syntactically and C++ drastically outperforms PHP when it comes to both CPU and memory usage. Even PHP itself is written in C. We knew that it was impossible to successfully rewrite an entire codebase of this size by hand, but wondered what would happen if we built a system to do it programmatically.

Finding new ways to improve PHP performance isn't a new concept. At run time the Zend Engine turns your PHP source into opcodes which are then run through the Zend Virtual Machine. Open source projects such as APC and eAccelerator cache this output and are used by the majority of PHP powered websites. There's also Zend Server, a commercial product which makes PHP faster via opcode optimization and caching. Instead, we were thinking about transforming PHP source directly into C++ which can then be turned into native machine code. Even compiling PHP isn't a new idea, open source projects like Roadsend and phc compile PHP to C, Quercus compiles PHP to Java, and Phalanger compiles PHP to .Net.

Needless to say, it took longer than that single Hackathon. Eight months later, I had enough code to demonstrate it is indeed possible to run faster with compiled code. We quickly added Iain Proctor and Minghui Yang to the team to speed up the pace of the project. We spent the next ten months finishing up all the coding and the following six months testing on production servers. We are proud to say that at this point, we are serving over 90% of our Web traffic using HipHop, all only six months after deployment.

How HipHop Works
The main challenge of the project was bridging the gap between PHP and C++. PHP is a scripting language with dynamic, weak typing. C++ is a compiled language with static typing. While PHP allows you to write magical dynamic features, most PHP is relatively straightforward. It's more likely that you see if (...) {...} else {..} than it is to see function foo($x) { include $x; }. This is where we gain in performance. Whenever possible our generated code uses static binding for functions and variables. We also use type inference to pick the most specific type possible for our variables and thus save memory.

The transformation process includes three main steps:

Static analysis where we collect information on who declares what and dependencies,
Type inference where we choose the most specific type between C++ scalars, String, Array, classes, Object, and Variant, and
Code generation which for the most part is a direct correspondence from PHP statements and expressions to C++ statements and expressions.

We have also developed HPHPi, which is an experimental interpreter designed for development. When using HPHPi you don't need to compile your PHP source code before running it. It's helped us catch bugs in HipHop itself and provides engineers a way to use HipHop without changing how they write PHP.

Overall HipHop allows us to keep the best aspects of PHP while taking advantage of the performance benefits of C++. In total, we have written over 300,000 lines of code and more than 5,000 unit tests. All of this will be released this evening on GitHub under the open source PHP license.

Learn More this Evening
This evening we're hosting a small group of developers to dive deeper into HipHop for PHP and will be streaming this tech talk live. Check back here around 7:30pm Pacific time if you'd like to watch.

As I'm sure there will be plenty of questions, starting this evening take a look at the HipHop wiki or join the HipHop developer mailing list. You'll also find us at FOSDEM, SCALE, PHP UK, ConFoo, TEK X, and OSCON over the next few months talking about HipHop for PHP. We're very excited to evolve HipHop into a thriving open source project along with all of you.


Haiping Zhao, a senior engineer, has found Facebook to be a programmer's paradise.

摘自:http://developers.facebook.com/news.php?blog=1&story=358

[PHP] 設定系統時區-putenv

[PHP] 設定系統時區-putenv
putenv("TZ=Asia/Taipei");
只要把這一行指令加到所建置的首頁上,系統時間就會被設定為台灣時區。
而且只要執行一次即可,下次要執行就等系統時間被設成別的時區or系統重置之後了。
如:
-----------------putenv使用方式-----------------
putenv("TZ=Asia/Taipei");
echo "台灣時間:". date("h:i:s")."\n";
$H = Date("H");//取得系統時間"時",然後指定給$H 變數
if ($H >=6 and $H <=18)
echo "早安,您好!";
else
echo "晚安,您好!";
?>
-----------------putenv使用方式-----------------

摘自:http://cordia-chin.blogspot.com/2009/10/php-putenv.html

2010年2月1日 星期一

Intersection of Date Ranges


function intersect($start1, $end1, $start2, $end2) {
return ($start1 >= $start2) ? ($start1 <= $end2) : ($start2 <= $end1);
}

$tests = array (
'First is on the far left ' => array(1, 2, 8, 9, False),
'First is on the far right' => array(8, 9, 1, 2, False),
'Identical ' => array(1, 9, 1, 9, True),
'Same start ' => array(1, 5, 1, 9, True),
'Same end ' => array(1, 9, 5, 9, True),
'First contains second ' => array(1, 9, 3, 7, True),
'Second contains first ' => array(3, 7, 1, 9, True),
'First end within second ' => array(1, 7, 3, 9, True),
'First start within second' => array(3, 9, 1, 7, True),
'First continues to second' => array(1, 5, 5, 9, True),
'Second continues to first' => array(5, 9, 1, 5, True),
);

foreach($tests as $test => $testData) {
list($start1, $end1, $start2, $end2, $expected) = $testData;
$intersects = intersect($start1, $end1, $start2, $end2);
echo
$test,
' Expected ', ($expected ? 'True ' : 'False'),
' Result ', ($intersects ? 'True ' : 'False'),
($expected === $intersects ? ' Passed' : ' Failed'),
PHP_EOL;
}

And the results ...

First is on the far left Expected False Result False Passed
First is on the far right Expected False Result False Passed
Identical Expected True Result True Passed
Same start Expected True Result True Passed
Same end Expected True Result True Passed
First contains second Expected True Result True Passed
Second contains first Expected True Result True Passed
First end within second Expected True Result True Passed
First start within second Expected True Result True Passed
First continues to second Expected True Result True Passed
Second continues to first Expected True Result True Passed


My only issue with this code is the ability to describe ranges that continue from each other.

The last 2 tests I would like to be able to return false on.

Range 1 is from 1 to 2
Range 2 is from 2 to 3

These could be considered as NON overlapping.

Currently both tests give the same result.

A word of warning, the ranges are inclusive. A range like 5,1 would mess things up a lot.



摘自:http://ryanfarley.com/blog/archive/2004/08/19/966.aspx

2010年1月29日 星期五

[桌面相關] XP 字體大改造!讓你的系統字型變得跟 MAC 一樣漂亮

本方法由《Jackbin 的懶人筆記本》提供。

Windows 一直以來都是以新細明體的面貌跟大家見面,不是作業系統或是網頁幾乎預設都是以這個字型居多,想必大家也都看習慣了吧!除了 Windows 之外, 蘋果 MAC 的內建字體受到許多網友的愛戴,但可惜都沒有簡單的方法能將蘋果字體套用到 Windows 裡作為系統字型,以至於大家都只能遠觀而不可褻玩焉。

透過 GDI++ 這套程式以及設計好的安裝批次檔,只要簡單的六個步驟就可以把新細明體換成蘋果的字體,安裝完成後的 XP 會變成這樣…




檔案下載:http://www.mediafire.com/?l2mywdwnmny

注意:如果你已經有安裝 GDI++ ,請先把他們移除,(包含【開始】 → 【所有程式】 → 【啟動】 → 【gditray.exe】)



Step 1

將下載下來的檔案解壓縮到 C 槽。



Step 2

接著打開 GDI++ 資料夾裡的「Font」→「_Replacer」資料夾,並點兩下滑鼠左鍵執行「Replacer.cmd」執行檔,會出現一個黑色的 DOS 視窗。



Step 3

開啟「C:\Windows\Font\」資料夾,找到「MingLiu&PMingliU」這個字型檔案(就是細明體&新細明體)。找到後用滑鼠左鍵選取該檔案後,拖曳到剛才開啟的黑色 DOS 視窗裡。

注意:拖曳檔案前先將該字型檔案複製到其他地方做備份。

這時 DOS 視窗理會顯示出這個字型的所在位置。按【Enter】下一步。



Step 4

將「C:\GDI++\font\iLihei」或「C:\GDI++\font\華文黑體」資料夾裡的「MINGLIU.TTC」字型檔拖曳到黑色 DOS 視窗,然後按【Enter】


(圖片引用自 Jackbin 的懶人筆記)

此時會出現「Continue? 」,在鍵盤上按「Y」然後按【Enter】。完成後會出現【Press any key to quit.】按任意鍵推出。


(圖片引用自 Jackbin 的懶人筆記)

Step 5

接著將「C:\GDI++\font」資料夾裡的「wt064.ttf」字型檔複製到「C:\Windows\Font\」資料夾裡面。

Step 6

最後,開啟「C:\GDI++\Registry」資料夾,執行裡面的「install_gdi++reg.bat」,這是要讓系統在開機時自動啟動 GDI++的步驟。

完成之後就重新啟動電腦吧!你會發現一切都變得不大一樣囉!




結果就是變成這樣:



不錯吧!

▍還原方法
(感謝網友 Leo 提供)

方法如下:

開啟「控制台」\「字型」。
刪除「MingLiU & PMingLiU(TrueType)」這個字型。
最後,將先前備份到桌面的「mingliu」字型,拉回控制台的「字型」。
重新開機,OK!

摘自:http://www.soft4fun.net/desktop/change-windows-font-to-mac-is-better-then-origin.htm

2010年1月27日 星期三

分享一個PHP的繁簡轉換類
Post by: LD | On Date: 一月 31st, 2009 | File Under PHP
這是一個很早的php繁簡轉換類,作者 CRLin 于2003年寫就,目前他留下的網址已不能訪問。原版中只要不同目錄調用就會出現 map 檔不能訪問的錯誤,我加入了 _FILE_DIR 常量,基本已沒有問題。
區別於 javascript方式,使用PHP轉換繁簡,可以方便的寫入資料庫中,其靈活度比較高。如果使用臺灣常用語與大陸常用語的對比map,可以一下子多出個繁文網站,我想做採集的諸位童鞋可能更感興趣。
使用方法:


include "big2gb.php";
$obj=new big2gb;
//簡體 轉換 繁體
$string_big5 = $obj->chg_utfcode($string_gb,'big5');
//繁體 轉換 簡體
$string_gb = $obj->chg_utfcode($string_big5,'gb2312'');


去 Ziddu 網盤下載 http://cn.ziddu.com/download/264698/bg2gb.zip.html
去 RayFile 網盤下載 http://www.rayfile.com/files/ac299599-ef3a-11dd-ba5e-0014221b798a/

喜悅村也有一個簡繁轉換的方法,
不妨一看: http://www.phpx.com/happy/viewthread.php?tid=66381

摘自:http://mifunny.info/share-a-php-class-of-character-conversion-for-gb2312-vs-big5-221.html

PHP minify

Combines, minifies, and caches JavaScript and CSS files on demand to speed up page loads.

Minify is a PHP5 app that helps you follow several of Yahoo!'s Rules for High Performance Web Sites.

It combines multiple CSS or Javascript files, removes unnecessary whitespace and comments, and serves them with gzip encoding and optimal client-side cache headers.

官網:http://code.google.com/p/minify/

Google的YSlow——Page Speed

先来看界面吧

和YSlow一样,“Page Speed”也是一个基于firebug附加组件的FireFox插件。虽然听起来有点拗口,但是意思很容易理解:如果你想用“Page Speed”,那么你就要安装firbug,而firebug是FireFox的一个附加组件,所以你也必须按照FireFox浏览器。同时另外一个意思是:IE!NO!Sorry!

“Page Speed”有两个面板,分别是“Page Speed”面板和“Page Speed Activity”面板。

“Page Speed”面板

Page Speed附加组件的Page Speed面板
和YSlow使用Yahoo的14条标准来衡量网页的综合速度一样,Page Speed通过Google指定的20条标准来衡量网页的综合速度。而Page Speed面板就是用来展现你的网页在Google20条标准上的得分。“Page Speed”通过分析你的网页加载、呈现速度,用20条标准来衡量,最终告知你的网页速度如何、哪种标准得分多少、问题所在、如何改进等信息。

“Page Speed Activity”面板

Page Speed附加组件的Page Speed Activity”面板
“Page Speed Activity”面板用于展现你的网页加载各种元素的所用时间,这样,你就可以更明确的知道到底是谁在浪费、占用大量的时间,从而更有针对性的进行改进。不同的阶段占用的时间,用不同的色块进行表示,恩,真是贴心的设计。

“Page Speed”的20条衡量标准

如果你对YSlow比较熟悉的话,那么一定会知道YSlow用于衡量网页速度的14条标准,而“Page Speed”有20条衡量标准,那么他们之间的到底有什么不同呢?Google又会给我们带来什么新的观点呢?

补充一下:如果你对YSlow的14条衡量标准不熟悉的话,您可以阅读一下,我以前写的两篇文章,分别是《如何提高网页的效率(上篇)——提高网页效率的14条准则》和《如何提高网页的效率(下篇)——Use YSlow to know why your web Slow》,文章较为详细的介绍了YSlow这个工具的使用,以及YSlow的14条衡量标准。这两篇文章同时也被收录到了《博客园精华集-web标准之道》一书当中。

继续补充:如果你想非常详细的了解YSlow的14条衡量标准的超详细讲解,那么你可以购买一本书,书的名字叫做:《高性能网站建设指南》,书的封面是一条经常出现在《人与兽》一类电影中的那种狗狗。


高性能网站建设指南
OK,不扯那么多了,让我们回到正题:“Page Speed”的20条衡量标准到底是什么呢?

Put CSS in the document head
将你的CSS样式表文件放在整个页面的头部。没有什么难理解的。css先下载下来,就能更快的渲染网页效果。从而让人们感觉网页速度很快。

更多关于"Put CSS in the document head"的更多详细解释,请看官方文档。

Use efficient CSS selectors
使用效率更高的CSS选择符。举个很简单的例子:尽量不要使用*号选择符:
*{padding:0;margin:0}
像这样的得分会很低,正确的办法应该是只对你想设置的标签元素进行设置,例如:
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
}
如果你对本条有更多的兴趣,可以去看Use efficient CSS selectors官方文档的详细解释。

Leverage proxy caching
代理缓存。这个名词听起来好像很屌的样子,以至于我也是查了资料才知道:所谓proxy cacheing,就是一种公共缓存,用于静态资源,允许浏览器从最近的代理服务器上,而不是从远程的原始服务器,下载这些静态资源。这些代理服务器,通常而言就是有ISP,接入服务商所提供的。

这样的代理服务器缓存可以让通过同一ISP接入服务的用户共享这些静态资源,而节省原始服务器的带宽,以及下载速度也会大大提高,特别是对于局域网的用户有特别的好处。

关于“Leverage proxy caching”的文档资料,请看官方文档的详细解释。

Minify JavaScript
最小化JavaScript脚本。这个貌似没有什么好说的,压缩一下你的JavaScript脚本吧。

关于“Minify JavaScript”的更多文档资料,请看官方文档的详细解释吧。

Optimize images
优化图片,其实最经常使用的就是css script了,也有翻译为“css精灵”的,虽然翻译的很美好,但是其实很简单,就是将多个图形,放在张图片文件中。这样,可以有效的减少http请求的数量。

如果你对“Optimize images”有更多的兴趣,可以看这个。一个css cript的示例: 《【CSS翻转门】技术实例讲解(附源码下载)》。

Minimize cookie size
最小化你的cookie。cookie的确是个好东西,他可以让你在用户的客户端保存一些东西,但是,千万不要什么都往用户口袋里面塞。原因很简单,cookie大小有限,有最大限制,而且cookie过大会减慢网页呈现的速度。另外为了安全性考虑,也不要把所有的什么破铜烂铁都塞到cookie里面。

关于“Minimize cookie size”的更多文档资料:看这里。

Enable gzip compression
使用gzip压缩。详细这个大家应该已经比较熟悉了。说白了就是服务器向浏览器发的是经过压缩的页面,这样传输的字节就会大大减少,速度自然也就快了。


这幅图说明了gzip技术的工作原理
欲了解更多的“Enable gzip compression”资料,请看官方文档。

Combine external JavaScript
合并外部的JavaScript文件。道理很简单,依然是为了少读取.js文件,从而有效的减少http请求数量。


合并外部的JavaScript文件可以有效的减少http请求数量
更多的关于“Combine external JavaScript”的文档,可以看这个。

Minimize DNS lookups
最小化DNS查询。详情请看这个:Minimize DNS lookups规则详情。

Optimize the order of styles and scripts
优化样式表和脚本的顺序。如果你要是看过老赵的《挣脱浏览器的束缚》系列的话(本系列也已经收录到《博客园精华集-web标准之道》一书中)。就知道IE浏览器对同一个域名下的文件,同时只能下载2个文件。所以,到底先让哪些样式表先下载下来,让哪些脚本先下载下来,这个顺序就非常重要了。所以,如果有可能,请重视一下样式表和脚本的顺序吧。推荐的做法是:将css放在js文件上面,让css文件先被加载,这样就可以先让网页渲染出来,从而加快浏览者的感知速度。


IE浏览器对同一个域名下的文件,同时只能下载2个文件
关于“Optimize the order of styles and scripts”更多详情,请看这个。

Serve resources from a consistent URL
相同的资源,使用相同的url地址。道理很好理解,如果是相同的一张图片,就不要东方一下,西方一下,然后引用的时候使用不同的url地址。为什么要这样做,道理也很简单,因为——缓存!

更多“Serve resources from a consistent URL”详情,请看这里

Avoid CSS expressions
避免CSS表达式。这个我在《如何提高网页的效率(上篇)——提高网页效率的14条准则》一文中也有讲到。现在需要补充的是:IE8已经不再支持css表达式功能。

关于“Avoid CSS expressions”,更多详情看这里。

Parallelize downloads across hostnames
通过不同的主机同时下载网页资源。这个的道理已经在“Optimize the order of styles and scripts”这一准则中讲述过。道理依然是老赵的《挣脱浏览器的束缚》提到的原因:IE浏览器对同一个域名下的文件,同时只能下载2个文件。

更多关于“Parallelize downloads across hostnames”的详情,可以看这个。

Combine external CSS
合并外部的css文件。这条和“Combine external JavaScript”准则的道理一样。还是为了减少http请求数量。

关于“Combine external CSS”的更多详情,可以看这个。

Specify image dimensions
明确的指明图片的高度和宽度。很久很久以前,long long ago。一个同学问我:“明确的指明图片的宽度和高度,是否能加快页面的渲染速度?”当时我的答案是:“这个应该没有关系吧!”。看来,当时的我是误人子弟了。明确的指出图片的高度和宽度,能够有效的加快浏览器在渲染图片周围布局和绘制呈现时的速度。

更多关于“Specify image dimensions”的详情,可以看这里。

Minimize redirects
尽量避免重定向。道理很简单,你从A地点到你的同事B先生家,到了那里,他的邻居告诉你,B先生已经搬家了,搬到了C地点,于是,你又跑到C地点,然后C地点有个人告诉你,B先生现在又搬家了,已经搬到了D地点。于是你又跑到了D地点,才找到了这个B同事。而这个装B的B同事,告诉你,你如果直接就来D地点,就不用那么麻烦了,而且速度也会更快一些。网页的跳转和重定向的道理是一样的。

关于“Minimize redirects”的详细文档,可以看这里。

Defer loading of JavaScript
延期加载JavaScript。这个听起来真是个高科技的东西呀。其实这玩意还真是非常的有效呀。不仅可以延期加载脚本,像一些大的图片、flash也都可以延期加载。其实实现原来也不是很难,就是先不加载一些比较大的东西,当页面加载完毕后,再加载那些东西。

关于“Defer loading of JavaScript”的更多详情,请看这里。

更多相关资料

关于“Page Speed”的更多相关阅读。重要的下载链接在这里:点击进入Page Speed下载页面,支持现在最新的FireFox3.5.2版本。

摘自:http://www.cnblogs.com/JustinYoung/archive/2009/08/10/Page-Speed-Google.html

wibiya widget