Programming/php
[php] inline string vs 兜字串的寫法 速度比較 / 測試 sample code
function timeFunc($function, $runs) { $times = array(); for ($i = 0; $i < $runs; $i++) { $time = microtime(); call_user_func($function); $times[$i] = microtime() - $time; } return array_sum($times) / $runs; } function Method1() { $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = "these are $foo"; } function Method2() { $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = "these are {$foo}"; } function Method3() { $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = sprintf("these are %s", $foo); } function Method4() { $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = "these are " . $foo; } function Method5() { $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = 'these are ' . $foo; } print timeFunc('Method1', 10) . "\n"; print timeFunc('Method2', 10) . "\n"; print timeFunc('Method3', 10) . "\n"; print timeFunc('Method4', 10) . "\n"; print timeFunc('Method5', 10) . "\n";
RESULT:
0.0023114
0.002422
0.0072015
0.001946
0.0017625
但是這是 loop 10萬次才有一點點的差距.
結論是 method5(單 quote + concate string) 速度較佳! method3 (sprintf) 最慘!
inline 寫法有兩種, 這兩種都差不多, 所以就 codeing 時的手順上, 以 $foo 這種是比較好寫這也是多數人的寫法 , {$foo} 這種比較好閱讀.
若是在需求上需要加上換行的話, 在一個 strings 中用 inline 寫法把 \n 放在 quote 中, 這種速度卻又比 concate string 寫法來得快.
ps: 速度比較這種題目很多人都會陷入某種迷失, 每個人的環境/需求都不盡相同, 並不是最快的就是適合自己的.
[php] array_key_exists vs isset 那一個比較快?
這些微差異要在大量的 loop 中才會有一些差別, 不過還是寫 code 時一次寫好吧 —> 給我自己的提醒!
答案是 isset 比較快!
ref. URL: http://www.alternateinterior.com/2006/11/comparing-array_key_exists-with-isset.html
相關資訊: A HOWTO on Optimizing PHP
http://phplens.com/lens/php-book/optimizing-debugging-php.php
Tuning Apache and PHP for Speed on Unix
http://phplens.com/phpeverywhere/tuning-apache-php
CodeIgniter MVC CRUD + memcache = Secret Message http://msg.monster.com.tw
這個 idea 是來自這個網站 : https://privnote.com/ , 簡單的說它是一個經由 https 保護傳送內容的網路服務 , user 用這個服務把臨時要給朋友的機密/私人資料譬如 password / URL 之類的 data 存放在這網站上, 然後此系統會給 user 一個唯一網址, 再把這網址給朋友, 開啟這個唯一網址後, 就可以看到這個訊息, 系統同時會把這訊息從系統中刪除… 哈哈! 看懂了嗎?
我的改進是用 memcache 的 expire 機制, 設定 10分鐘後, 若 user 沒讀過此訊息, 訊息會 ‘自動’ 銷毀… 我的站若再去申請 ssl 加密的話就跟那個站功能是類似的了.
codeigniter 的 MVC 架構, 讓我很快的把這個 idea implement 出來了 , 基本上就是一個簡單版的 CRUD
我的 Secret Message 服務長像非常的陽春 😛 , 僅用簡單的 HTML , 若有空再加上 style 美化一下版面, 不過基本功能是有的了, 大家用看看, 有 idea 或意見請再告訴我.
目前 message 不提供 HTML / VBB , 但是有簡單的 skype 版表情符號 ( icon 正在慢慢搜集中 ) ….
Secret Message 網址是: http://msg.monster.com.tw/
CI 表情符號改寫例:
用 php code 產生 1×1 pixel 的 小黑點 sample code
這個可以用來做 tracking 輸出用
header("Content-Type: image/gif"); header("Content-Length: 49"); echo pack('H*', '47494638396101000100910000000000ffffffff' .'ffff00000021f90405140002002c000000000100' .'01000002025401003b' );
PHP / MySQL 一些容易被忽略的 Optimization
URL : http://www.dublish.com/articles/10.html
- Use NOT NULL as default value as much as you can, it speeds up execution and saves one bit.
- 一個 <? … ?> 比多個 <? .. ?><? .. ?><? .. ?> 還快
- 少用 . 去串接字串, 改用這樣的較快 “select addr,name from tbl_addr where id=$id”
- 或者用 ‘ “ 區分出 需要 PHP 不解譯/解譯的字串
- echo 比 print 快
- 在 loop 前就把終值先算好, 放變數裡,NG: for($i=0; $i<strlen($str); $i++) ….
[php] regular expression match
preg_match( '/c=([0-9]+)/' , $t->log['dest_query'] , $data); 這樣可以取出 c=xxxx
抓 user agent 版本
preg_match( '/MSIE ([0-9]+.[0-9])/' , $_SERVER['HTTP_USER_AGENT'] , $data_msie_ver); preg_match( '/Windows NT ([0-9]+.[0-9])/' , $_SERVER['HTTP_USER_AGENT'] , $data_nt_ver); if ( !( (float)$data_msie_ver[1]>=6 && (float)$data_msie_ver[1]<=8 ) ) $valid_useragent = 0;