fwrite 和file_put_contents谁更快?

jackxiang 2013-9-27 13:27 | |
fwrite 和file_put_contents谁更快? LOCK_EX
发表于 2011年05月12日 由 nick  
/**
* author:hanyh2004@gmail.com
* copyright:转载请注明出处
*/
fwrite 和file_put_contents谁更快?
==========================================
看两者的函数原型:
int fwrite ( resource $handle, string $string [, int $length] )
int file_put_contents ( string $filename, mixed $data [, int $flags [, resource $context]] )

fwrite简单的把数据写到handler里面
file_put_contents可能需要处理contenxt,数据类型为mixed,需要更多处理
虽 然看file_put_contents的函数说明:This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

写个简单程序测试一下,一个240M文件
==========================================
<?php
$len = 1024*1024*24;
$data = str_repeat(“-”,$len);

$start = microtime(true);
$fp = fopen(“/tmp/b”,”w”);
fwrite($fp,$data,$len);
fclose($fp);
$end = microtime(true);
echo “elipsed time:”.($end-$start).”\n”;

$start = microtime(true);
file_put_contents(“/tmp/a”,$data);
$end = microtime(true);
echo “elipsed time:”.($end-$start).”\n”;

god@god-desktop:~/projects/php$ php fwrite_VS_file_put_contents.php
elipsed time:6.0958020687103
elipsed time:9.6280250549316
god@god-desktop:~/projects/php$ php fwrite_VS_file_put_contents.php
elipsed time:6.247565984726
elipsed time:9.0449070930481

结论:多次执行结果类试,说明fopen,fwrite,fclose方式比直接file_put_contentx要快一点!

Then why? 查看source
===========================================
直接apt-get source php5
解压:cd /home/god/projects/php/php5-5.3.3

fwrite
——————————————-
1查找函数fwrite 函数:god@god-desktop:~/projects/php/php5-5.3.3$ grep -rn “PHP_FUNCTION(fwrite)” .
./ext/standard/file.c:1233:PHPAPI PHP_FUNCTION(fwrite)
./ext/standard/file.h:43:PHPAPI PHP_FUNCTION(fwrite);

找到对应源码,该函数非常简单:

PHPAPI PHP_FUNCTION(fwrite)
{
zval *arg1;
char *arg2;
int arg2len;
int ret;
int num_bytes;
long arg3 = 0;
char *buffer = NULL;
php_stream *stream;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “rs|l”, &arg1, &arg2, &arg2len, &arg3) == FAILURE) {
RETURN_FALSE;
}

if (ZEND_NUM_ARGS() == 2) {
num_bytes = arg2len;
} else {
num_bytes = MAX(0, MIN((int)arg3, arg2len));
}

if (!num_bytes) {
RETURN_LONG(0);
}

PHP_STREAM_TO_ZVAL(stream, &arg1);

if (PG(magic_quotes_runtime)) {
buffer = estrndup(arg2, num_bytes);
php_stripslashes(buffer, &num_bytes TSRMLS_CC);
}
ret = php_stream_write(stream, buffer ? buffer : arg2, num_bytes);
if (buffer) {
efree(buffer);
}

RETURN_LONG(ret);
}

file_put_contents
——————————————-
该函数的处理操作就多多了
PHP_FUNCTION(file_put_contents)
{
php_stream *stream;
char *filename;
int filename_len;
zval *data;
int numbytes = 0;
long flags = 0;
zval *zcontext = NULL;
php_stream_context *context = NULL;
php_stream *srcstream = NULL;
char mode[3] = “wb”;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “sz/|lr!”, &filename, &filename_len, &data, &flags, &zcontext) == FAILURE) {
return;
}
if (Z_TYPE_P(data) == IS_RESOURCE) {
php_stream_from_zval(srcstream, &data);
}

context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);

if (flags & PHP_FILE_APPEND) {
mode[0] = ‘a’;
} else if (flags & LOCK_EX) {
/* check to make sure we are dealing with a regular file */
if (php_memnstr(filename, “://”, sizeof(“://”) – 1, filename + filename_len)) {
if (strncasecmp(filename, “file://”, sizeof(“file://”) – 1)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Exclusive locks may only be set for regular files”);
RETURN_FALSE;
}
}
mode[0] = ‘c’;
}
mode[2] = ‘\0′;

stream = php_stream_open_wrapper_ex(filename, mode, ((flags & PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
if (stream == NULL) {
RETURN_FALSE;
}
if (flags & LOCK_EX && (!php_stream_supports_lock(stream) || php_stream_lock(stream, LOCK_EX))) {
php_stream_close(stream);
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Exclusive locks are not supported for this stream”);
RETURN_FALSE;
}

if (mode[0] == ‘c’) {
php_stream_truncate_set_size(stream, 0);
}

switch (Z_TYPE_P(data)) {
case IS_RESOURCE: {
size_t len;
if (php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL, &len) != SUCCESS) {
numbytes = -1;
} else {
numbytes = len;
}
break;
}
case IS_NULL:
case IS_LONG:
case IS_DOUBLE:
case IS_BOOL:
case IS_CONSTANT:
convert_to_string_ex(&data);

case IS_STRING:
if (Z_STRLEN_P(data)) {
numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data));
if (numbytes != Z_STRLEN_P(data)) {
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));
numbytes = -1;
}
}
break;

case IS_ARRAY:
if (zend_hash_num_elements(Z_ARRVAL_P(data))) {
int bytes_written;
zval **tmp;
HashPosition pos;

zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(data), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(data), (void **) &tmp, &pos) == SUCCESS) {
if (Z_TYPE_PP(tmp) != IS_STRING) {
SEPARATE_ZVAL(tmp);
convert_to_string(*tmp);
}
if (Z_STRLEN_PP(tmp)) {
numbytes += Z_STRLEN_PP(tmp);
bytes_written = php_stream_write(stream, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
if (bytes_written < 0 || bytes_written != Z_STRLEN_PP(tmp)) {
if (bytes_written < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Failed to write %d bytes to %s”, Z_STRLEN_PP(tmp), filename);

} else {
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));
}
numbytes = -1;
break;
}
}
zend_hash_move_forward_ex(Z_ARRVAL_P(data), &pos);
}
}
break;

case IS_OBJECT:
if (Z_OBJ_HT_P(data) != NULL) {
zval out;
//看看对像怎么保存的:)
if (zend_std_cast_object_tostring(data, &out, IS_STRING TSRMLS_CC) == SUCCESS) {
numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRLEN(out));
if (numbytes != Z_STRLEN(out)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Only %d of %d bytes written, possibly out of free disk space”, numbytes, Z_STRLEN(out));
numbytes = -1;
}
zval_dtor(&out);
break;
}
}
default:
numbytes = -1;
break;
}
php_stream_close(stream);

if (numbytes < 0) {
RETURN_FALSE;
}

RETURN_LONG(numbytes);
}

什么时候用fwrite,file_put_contents ?
======================================
1,函数原型已经说明了它们处理的数据类型不一样
2,简单的文件处理,追求速度用fwrite
3,书写简单用file_put_contents(啥类型的数据都能处理,magic阿。但是要理解类型判断机制,否则保存的数据可能不是你想要的)

LOCK_EX : http://bugs.php.net/43182

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/6704/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]