<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[fwrite 和file_put_contents谁更快？]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Fri, 27 Sep 2013 05:27:21 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	fwrite 和file_put_contents谁更快？ LOCK_EX<br/>发表于 2011年05月12日 由 nick&nbsp;&nbsp;<br/>/**<br/>* author:hanyh2004@gmail.com<br/>* copyright:转载请注明出处<br/>*/<br/>fwrite 和file_put_contents谁更快？<br/>==========================================<br/>看两者的函数原型:<br/>int fwrite ( resource $handle, string $string [, int $length] )<br/>int file_put_contents ( string $filename, mixed $data [, int $flags [, resource $context]] )<br/><br/>fwrite简单的把数据写到handler里面<br/>file_put_contents可能需要处理contenxt，数据类型为mixed，需要更多处理<br/>虽 然看file_put_contents的函数说明:This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.<br/><br/>写个简单程序测试一下,一个240M文件<br/>==========================================<br/>&lt;?php<br/>$len = 1024*1024*24;<br/>$data = str_repeat(“-”,$len);<br/><br/>$start = microtime(true);<br/>$fp = fopen(“/tmp/b”,”w”);<br/>fwrite($fp,$data,$len);<br/>fclose($fp);<br/>$end = microtime(true);<br/>echo “elipsed time:”.($end-$start).”&#92;n”;<br/><br/>$start = microtime(true);<br/>file_put_contents(“/tmp/a”,$data);<br/>$end = microtime(true);<br/>echo “elipsed time:”.($end-$start).”&#92;n”;<br/><br/>god@god-desktop:~/projects/php$ php fwrite_VS_file_put_contents.php<br/>elipsed time:6.0958020687103<br/>elipsed time:9.6280250549316<br/>god@god-desktop:~/projects/php$ php fwrite_VS_file_put_contents.php<br/>elipsed time:6.247565984726<br/>elipsed time:9.0449070930481<br/>…<br/>结论：多次执行结果类试，说明fopen,fwrite,fclose方式比直接file_put_contentx要快一点！<br/><br/>Then why? 查看source<br/>===========================================<br/>直接apt-get source php5<br/>解压：cd /home/god/projects/php/php5-5.3.3<br/><br/>fwrite<br/>——————————————-<br/>1查找函数fwrite 函数:god@god-desktop:~/projects/php/php5-5.3.3$ grep -rn “PHP_FUNCTION(fwrite)” .<br/>./ext/standard/file.c:1233:PHPAPI PHP_FUNCTION(fwrite)<br/>./ext/standard/file.h:43:PHPAPI PHP_FUNCTION(fwrite);<br/><br/>找到对应源码，该函数非常简单:<br/><br/>PHPAPI PHP_FUNCTION(fwrite)<br/>&#123;<br/>zval *arg1;<br/>char *arg2;<br/>int arg2len;<br/>int ret;<br/>int num_bytes;<br/>long arg3 = 0;<br/>char *buffer = NULL;<br/>php_stream *stream;<br/><br/>if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “rs&#124;l”, &amp;arg1, &amp;arg2, &amp;arg2len, &amp;arg3) == FAILURE) &#123;<br/>RETURN_FALSE;<br/>&#125;<br/><br/>if (ZEND_NUM_ARGS() == 2) &#123;<br/>num_bytes = arg2len;<br/>&#125; else &#123;<br/>num_bytes = MAX(0, MIN((int)arg3, arg2len));<br/>&#125;<br/><br/>if (!num_bytes) &#123;<br/>RETURN_LONG(0);<br/>&#125;<br/><br/>PHP_STREAM_TO_ZVAL(stream, &amp;arg1);<br/><br/>if (PG(magic_quotes_runtime)) &#123;<br/>buffer = estrndup(arg2, num_bytes);<br/>php_stripslashes(buffer, &amp;num_bytes TSRMLS_CC);<br/>&#125;<br/>ret = php_stream_write(stream, buffer ? buffer : arg2, num_bytes);<br/>if (buffer) &#123;<br/>efree(buffer);<br/>&#125;<br/><br/>RETURN_LONG(ret);<br/>&#125;<br/><br/>file_put_contents<br/>——————————————-<br/>该函数的处理操作就多多了<br/>PHP_FUNCTION(file_put_contents)<br/>&#123;<br/>php_stream *stream;<br/>char *filename;<br/>int filename_len;<br/>zval *data;<br/>int numbytes = 0;<br/>long flags = 0;<br/>zval *zcontext = NULL;<br/>php_stream_context *context = NULL;<br/>php_stream *srcstream = NULL;<br/>char mode[3] = “wb”;<br/><br/>if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “sz/&#124;lr!”, &amp;filename, &amp;filename_len, &amp;data, &amp;flags, &amp;zcontext) == FAILURE) &#123;<br/>return;<br/>&#125;<br/>if (Z_TYPE_P(data) == IS_RESOURCE) &#123;<br/>php_stream_from_zval(srcstream, &amp;data);<br/>&#125;<br/><br/>context = php_stream_context_from_zval(zcontext, flags &amp; PHP_FILE_NO_DEFAULT_CONTEXT);<br/><br/>if (flags &amp; PHP_FILE_APPEND) &#123;<br/>mode[0] = ‘a’;<br/>&#125; else if (flags &amp; LOCK_EX) &#123;<br/>/* check to make sure we are dealing with a regular file */<br/>if (php_memnstr(filename, “://”, sizeof(“://”) – 1, filename + filename_len)) &#123;<br/>if (strncasecmp(filename, “file://”, sizeof(“file://”) – 1)) &#123;<br/>php_error_docref(NULL TSRMLS_CC, E_WARNING, “Exclusive locks may only be set for regular files”);<br/>RETURN_FALSE;<br/>&#125;<br/>&#125;<br/>mode[0] = ‘c’;<br/>&#125;<br/>mode[2] = ‘&#92;0′;<br/><br/>stream = php_stream_open_wrapper_ex(filename, mode, ((flags &amp; PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) &#124; ENFORCE_SAFE_MODE &#124; REPORT_ERRORS, NULL, context);<br/>if (stream == NULL) &#123;<br/>RETURN_FALSE;<br/>&#125;<br/>if (flags &amp; LOCK_EX &amp;&amp; (!php_stream_supports_lock(stream) &#124;&#124; php_stream_lock(stream, LOCK_EX))) &#123;<br/>php_stream_close(stream);<br/>php_error_docref(NULL TSRMLS_CC, E_WARNING, “Exclusive locks are not supported for this stream”);<br/>RETURN_FALSE;<br/>&#125;<br/><br/>if (mode[0] == ‘c’) &#123;<br/>php_stream_truncate_set_size(stream, 0);<br/>&#125;<br/><br/>switch (Z_TYPE_P(data)) &#123;<br/>case IS_RESOURCE: &#123;<br/>size_t len;<br/>if (php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL, &amp;len) != SUCCESS) &#123;<br/>numbytes = -1;<br/>&#125; else &#123;<br/>numbytes = len;<br/>&#125;<br/>break;<br/>&#125;<br/>case IS_NULL:<br/>case IS_LONG:<br/>case IS_DOUBLE:<br/>case IS_BOOL:<br/>case IS_CONSTANT:<br/>convert_to_string_ex(&amp;data);<br/><br/>case IS_STRING:<br/>if (Z_STRLEN_P(data)) &#123;<br/>numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data));<br/>if (numbytes != Z_STRLEN_P(data)) &#123;<br/>php_error_docref(NULL TSRMLS_CC, E_WARNING, “Only %d of %d bytes written, possibly out of free disk space”, numbytes, Z_STRLEN_P(data));<br/>numbytes = -1;<br/>&#125;<br/>&#125;<br/>break;<br/><br/>case IS_ARRAY:<br/>if (zend_hash_num_elements(Z_ARRVAL_P(data))) &#123;<br/>int bytes_written;<br/>zval **tmp;<br/>HashPosition pos;<br/><br/>zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(data), &amp;pos);<br/>while (zend_hash_get_current_data_ex(Z_ARRVAL_P(data), (void **) &amp;tmp, &amp;pos) == SUCCESS) &#123;<br/>if (Z_TYPE_PP(tmp) != IS_STRING) &#123;<br/>SEPARATE_ZVAL(tmp);<br/>convert_to_string(*tmp);<br/>&#125;<br/>if (Z_STRLEN_PP(tmp)) &#123;<br/>numbytes += Z_STRLEN_PP(tmp);<br/>bytes_written = php_stream_write(stream, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));<br/>if (bytes_written &lt; 0 &#124;&#124; bytes_written != Z_STRLEN_PP(tmp)) &#123;<br/>if (bytes_written &lt; 0) &#123;<br/>php_error_docref(NULL TSRMLS_CC, E_WARNING, “Failed to write %d bytes to %s”, Z_STRLEN_PP(tmp), filename);<br/><br/>&#125; else &#123;<br/>php_error_docref(NULL TSRMLS_CC, E_WARNING, “Only %d of %d bytes written, possibly out of free disk space”, bytes_written, Z_STRLEN_PP(tmp));<br/>&#125;<br/>numbytes = -1;<br/>break;<br/>&#125;<br/>&#125;<br/>zend_hash_move_forward_ex(Z_ARRVAL_P(data), &amp;pos);<br/>&#125;<br/>&#125;<br/>break;<br/><br/>case IS_OBJECT:<br/>if (Z_OBJ_HT_P(data) != NULL) &#123;<br/>zval out;<br/>//看看对像怎么保存的:)<br/>if (zend_std_cast_object_tostring(data, &amp;out, IS_STRING TSRMLS_CC) == SUCCESS) &#123;<br/>numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRLEN(out));<br/>if (numbytes != Z_STRLEN(out)) &#123;<br/>php_error_docref(NULL TSRMLS_CC, E_WARNING, “Only %d of %d bytes written, possibly out of free disk space”, numbytes, Z_STRLEN(out));<br/>numbytes = -1;<br/>&#125;<br/>zval_dtor(&amp;out);<br/>break;<br/>&#125;<br/>&#125;<br/>default:<br/>numbytes = -1;<br/>break;<br/>&#125;<br/>php_stream_close(stream);<br/><br/>if (numbytes &lt; 0) &#123;<br/>RETURN_FALSE;<br/>&#125;<br/><br/>RETURN_LONG(numbytes);<br/>&#125;<br/><br/>什么时候用fwrite,file_put_contents ?<br/>======================================<br/>1，函数原型已经说明了它们处理的数据类型不一样<br/>2，简单的文件处理，追求速度用fwrite<br/>3，书写简单用file_put_contents(啥类型的数据都能处理，magic阿。但是要理解类型判断机制，否则保存的数据可能不是你想要的）<br/><br/>LOCK_EX : http://bugs.php.net/43182<br/>
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] fwrite 和file_put_contents谁更快？]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>