PHP性能分析(001)-str_repeat ($input, $multiplier)

WBOY
发布: 2016-08-08 09:29:25
原创
1013 人浏览过

<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> $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> $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> 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></code>
登录后复制

以上就介绍了PHP性能分析(001)-str_repeat ($input, $multiplier),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板