3 ms·
I think you still need to do the templating in PHP, because you usually want to escape html chars. And because you want to seperate code and layout. Having "Hel
by mg 2y ago
I think you still need to do the templating in PHP, because you usually want to escape html chars. And because you want to seperate code and layout. Having "Hello <?=$name?>" in the template is usually not what you want. It is also harder to read than "Hello {NAME}".
Let's test templating in Python and PHP!
template.py:
def main():
t = 'Number {NR} is cool'
i = 10_000_000
r = 0
while i>0:
x = t.replace('{NR}', str(i))
r += len(x)
i = i - 1
print(r)
main()
template.php:
<?php
$t = 'Number {NR} is cool';
$i = 10_000_000;
$r = 0;
while ($i>0) {
$x = str_replace('{NR}', $i, $t);
$r += strlen($x);
$i = $i - 1;
}
print($r);
Results:
time python3 template.py
218888897
real 0m2.854s
time php template.php
218888897
real 0m0.994s
time pypy3 template.py
218888897
real 0m0.795s
So for this use case, PHP is 3x faster than CPython and PyPy is 20% faster than PHP.