PHP程式碼生成器介紹
PHP generators are awesome
I’m not sure if you heard but PHP 5.4 is going to support generators. This is fantastic news if you ask me.
Although PHP 5.3 has been out a while, few hosting providers have fully adopted it so I have little hope getting this awesomeness on the burner just yet – so I put together a little script. very similar to the yield functionality that will be implemented by our good friends at PHP.
For those python/Vala/Ruby/Java people out there this may not look like a big deal but it is… It shows that PHP, with all of its faults, is becoming a better language every day.
This script. by no means optimized and should be seen only as a way to possibly implement Yielding in PHP and how to use in Magento for a finer and better data interaction with exports and imports. Notice that I unset the data as much as possible even tough the garbage collector should take care of it.
I’m not sure if you heard but PHP 5.4 is going to support generators. This is fantastic news if you ask me.
Although PHP 5.3 has been out a while, few hosting providers have fully adopted it so I have little hope getting this awesomeness on the burner just yet – so I put together a little script. very similar to the yield functionality that will be implemented by our good friends at PHP.
For those python/Vala/Ruby/Java people out there this may not look like a big deal but it is… It shows that PHP, with all of its faults, is becoming a better language every day.
This script. by no means optimized and should be seen only as a way to possibly implement Yielding in PHP and how to use in Magento for a finer and better data interaction with exports and imports. Notice that I unset the data as much as possible even tough the garbage collector should take care of it.
As you can see there are 2 different versions, one for < 5.3 and one for 5.3 and greater. Since I can’t do math in my head I added an extra convert function and you should know that little function adds more memory to the mix… so take it out if you think your data is skewed. Try reading a file with this approach or a big array.
[code]<!--?php
echo "Current Memory: " . convert(memory_get_usage(true)), "
function convert($size) {
$unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}
function yield() {
//echo 'Memory before processing the data: ', memory_get_usage(),' in ',FUNCTION, ':
';
$args = func_get_args();
//do something with data
//echo '
';
//var_dump($args);
// echo '
';
// echo 'Memory after processing the data: ', memory_get_usage(),' in ',FUNCTION, ':
';
unset($args);
return;
}
function process(array $someData = array()) {
// do some computations that generates $data
foreach ($someData as $data) {
// echo 'Memory before yielding: ', memory_get_usage(),' in ',FUNCTION, ':
';
call_user_func('yield', $data);
// echo 'Memory after yielding: ', memory_get_usage(),' in ',FUNCTION, ':
';
}
}
function process_53(array $someData = array()) {
// do some computations that generates $data
foreach ($someData as $data) {
// echo 'Memory before yielding: ', memory_get_usage(),' in ',FUNCTION, ':
';
call_user_func(function() {
//echo 'Memory before processing the data: ', memory_get_usage(),' in ',FUNCTION, ':
';
$args = func_get_args();
//do something with data
//echo '
';
//var_dump($args);
// echo '
';
// echo 'Memory after processing the data: ', memory_get_usage(),' in ',FUNCTION, ':
';
unset($args);
return;
}, $data);
// echo 'Memory after yielding: ', memory_get_usage(),' in ',FUNCTION, ':
';
}
}
echo '
Let\'s do something fun
';
echo '
Starting memory: ', convert(memory_get_usage(true)), '
';
$longString = str_repeat(md5('php yield'), 2000); //heavy operation
$someData = array_fill(5, 60000, $longString); // this alone will raise your memory usage to the moon
echo '
Memory after getting the data: ', convert(memory_get_usage(true)), '
';
process_53($someData);
unset($someData);
echo '
End memory: ', convert(memory_get_usage(true)), '
';
[/code]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-742850/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- php介紹PHP
- OutputStreamWriter介紹&程式碼實現和InputStreamReader介紹&程式碼實現
- PHP 5.4介紹PHP
- 程式碼大全介紹 (轉)
- CNN介紹及程式碼實現CNN
- BiLSTM介紹及程式碼實現
- $.ajax()用法例項程式碼介紹
- webpack指南-webpack介紹-程式碼分割Web
- 介紹Nginx配置支援PHPNginxPHP
- 程式碼生成器
- mqtt介紹和go程式碼實現MQQTGo
- SVG程式碼構成簡單介紹SVG
- $.getJSON()用法程式碼例項介紹JSON
- javascript程式碼規範簡單介紹JavaScript
- 簡單介紹python迭代器和生成器Python
- PHP-CodeIgniter介紹PHP
- Laravel 程式碼生成器Laravel
- js 程式碼生成器JS
- ZT 程式碼生成器
- PHP Web頁面安全生成器指令碼PHPWeb指令碼
- Google程式碼評審介紹 - Michaela GreilerGo
- 代理IP常見錯誤程式碼介紹
- http代理401錯誤程式碼介紹HTTP
- 常用正規表示式匹配程式碼介紹
- javascript程式碼效能優化簡單介紹JavaScript優化
- javascript事件控制程式碼簡單介紹JavaScript事件
- 如何除錯javascript程式碼簡單介紹除錯JavaScript
- PHP的靜態變數介紹PHP變數
- php常用Stream函式集介紹PHP函式
- DISCUZ原始碼分析流程詳細介紹【admin.php入口】原始碼PHP
- go模板-程式碼生成器Go
- MyBatis Plus程式碼生成器MyBatis
- PHP 程式設計師開發比特幣入門介紹PHP程式設計師比特幣
- PHP生成器模式PHP模式
- python 中的迭代器和生成器簡單介紹Python
- CGI FastCGI PHP-CGI與PHP-FPM概念介紹ASTPHP
- SwiftLint:程式碼規範檢查工具介紹Swift
- Laravel核心程式碼學習 -- Database 基礎介紹LaravelDatabase