Home >Backend Development >PHP Tutorial >PHP performance analysis (001)-str_repeat ($input, $multiplier)

PHP performance analysis (001)-str_repeat ($input, $multiplier)

WBOY
WBOYOriginal
2016-08-08 09:29:251036browse

<code><span><span><span><?php</span>
header(<span>"Pramga: no-cache"</span>);

<span>/**
 * 获得空心金字塔:<br/>
 * 1.使用了memcpy和memmove,效率最高。
 *
 *<span> @param</span> number $levelNum
 *            金字塔层数:默认20
 *<span> @return</span> string 金字塔的结果
 */</span><span><span>function</span><span>getEmptyJinZiTa</span><span>(<span>$levelNum</span> = <span>20</span>)</span>
{</span><span>$SPACE</span> = <span>" "</span>; <span>// 用于填充空格</span><span>$STAR</span> = <span>"*"</span>; <span>// 用于填充型号</span><span>$decreseSpace</span> = <span>$levelNum</span> - <span>1</span>; <span>// 第一行开始的空格,逐行递减</span><span>$result</span> = <span>""</span>;
    <span>for</span> (<span>$level</span> = <span>1</span>; <span>$level</span> <= <span>$levelNum</span>; ++ <span>$level</span>) { <span>// 循环外层数</span><span>$result</span> .= str_repeat(<span>$SPACE</span>, <span>$decreseSpace</span> --);
        <span>if</span> (<span>$level</span> >= <span>1</span> && <span>$level</span> < <span>$levelNum</span>) {
            <span>$result</span> .= <span>$STAR</span>;
            <span>$spaceNum</span> = (<span>$level</span> - <span>1</span>) * <span>2</span> - <span>1</span>;
            <span>$result</span> .= str_repeat(<span>$SPACE</span>, <span>$spaceNum</span> < <span>0</span> ? <span>0</span> : <span>$spaceNum</span>);
            <span>if</span> (<span>$level</span> != <span>1</span>) <span>// 第一行就一个*</span><span>$result</span> .= <span>$STAR</span>;
        } <span>else</span><span>if</span> (<span>$level</span> == <span>$levelNum</span>) { <span>// 最底层需要填充全*</span><span>$result</span> .= str_repeat(<span>$STAR</span>, <span>$level</span> * <span>2</span> - <span>1</span>);
            }
        <span>$result</span> .= <span>"<br/>"</span>;
    }
    <span>return</span><span>$result</span>;
}

<span>echo</span> getEmptyJinZiTa();<span>//打印金字塔重点内容</span><span>?></span></span></code>

The above introduces PHP performance analysis (001)-str_repeat ($input, $multiplier), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn