我正在做一个 Hack 项目,遇到了需要打印 $n
的情况。空间。这是我目前的做法:
for ($i = 0; $i < $n; $i++) echo " ";
我想知道是否
$n
调用 echo 是最有效的方法吗?通过一点点谷歌搜索,我了解到,通常,多次调用 echo 比字符串连接更快,而且 Hack 没有内置的 StringBuilder 等效项。我的 for 循环是否达到了最高效率,还是我还缺少其他东西?谢谢!
最佳答案
如果你使用 HackLang,你应该使用 HSL
( Hack standard library )而不是遗留的 php 函数。
解决此问题的最佳方法是使用 Str\repeat
函数(与 PHP 中 str_repeat
的行为类似)
use namespace HH\Lib\Str;
echo Str\repeat(' ', $n);
Note: make sure to use the same HSL version as HHVM
if you are using HHVM 4 ( recommended ), do
composer require hhvm/hsl:^4
if you are using HHVM 3, do
hhvm $(which composer) require hhvm/hsl:^3
etc...
关于hhvm - 在 Hack 中打印可变数量字符的最有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56260740/