iliaa Thu Oct 3 09:59:31 2002 EDT Modified files: /php4/ext/standard string.c Log: Optimized str_repeat() function, it is now 10x faster on 1 byte multipliers and 5-6x faster on multi-byte multipliers. Index: php4/ext/standard/string.c diff -u php4/ext/standard/string.c:1.306 php4/ext/standard/string.c:1.307 --- php4/ext/standard/string.c:1.306 Thu Oct 3 00:56:54 2002 +++ php4/ext/standard/string.c Thu Oct 3 09:59:31 2002 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: string.c,v 1.306 2002/10/03 04:56:54 yohgaki Exp $ */ +/* $Id: string.c,v 1.307 2002/10/03 13:59:31 iliaa Exp $ */ /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */ @@ -3537,10 +3537,9 @@ PHP_FUNCTION(str_repeat) { zval **input_str; /* Input string */ - zval **mult; /* Multiplier */ - char *result; /* Resulting string */ - int result_len; /* Length of the resulting string */ - int i; + zval **mult; /* Multiplier */ + char *result; /* Resulting string */ + int result_len; /* Length of the resulting string */ if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &input_str, &mult) == FAILURE) { WRONG_PARAM_COUNT; @@ -3567,12 +3566,24 @@ result_len = Z_STRLEN_PP(input_str) * Z_LVAL_PP(mult); result = (char *)emalloc(result_len + 1); - /* Copy the input string into the result as many times as necessary */ - for (i = 0; i < Z_LVAL_PP(mult); i++) { - memcpy(result + Z_STRLEN_PP(input_str) * i, - Z_STRVAL_PP(input_str), - Z_STRLEN_PP(input_str)); + /* Heavy optimization for situations where multiplier is 1 byte long */ + if (Z_LVAL_PP(mult) == 1) { + memset(result, *(Z_STRVAL_PP(input_str)), Z_LVAL_PP(mult)); + } else { + char *s, *e, *ee; + int l=0; + memcpy(result, Z_STRVAL_PP(input_str), Z_STRLEN_PP(input_str)); + s = result; + e = result + Z_STRLEN_PP(input_str); + ee = result + result_len; + + while (e<ee) { + l = (e-s) < (ee-e) ? (e-s) : (ee-e); + memmove(e, s, l); + e += l; + } } + result[result_len] = '\0'; RETURN_STRINGL(result, result_len, 0);
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php