Suddenly it became interesting - are there any differences in the speed of the php script:
if(1!=1){ echo "Ошибка"; exit; } /* дальше много - много кода */ or
if(1==1) { /* много - много кода */ } else { echo "Ошибка"; } Suddenly it became interesting - are there any differences in the speed of the php script:
if(1!=1){ echo "Ошибка"; exit; } /* дальше много - много кода */ or
if(1==1) { /* много - много кода */ } else { echo "Ошибка"; } If you can not measure, you just need to think.
If the function code is first read entirely before execution and compiled into some form for the interpreter, there will be no difference in the execution time.
And if you interpret the code "in the forehead" ie immediately as you read it, the first form
if (condition) {... exit;} no doubt it will work faster (if the condition in if == true).
So the question comes down to - "How does the PHP interpreter work?"
-
My guess is - there will be no time difference.
-
One more note - you should not do measurements when in if constant expression.
Update
I see in the comments the lack of understanding of the text of the answer.
In the summary, I wrote - "My guess is - there will be no time difference."
This means that some kind of preliminary compilation of the function body is carried out, at least all its text will be read before execution, i.e. in reality, the code is not executed while reading.
For confirmation - a small example
avp@avp-xub11:hashcode$ cat t.php <?php if(1!=1) { echo "1\n" echo Ошибка1; } else { echo "2\n"; echo Ошибка2; } ?> avp@avp-xub11:hashcode$ php <t.php PHP Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in - on line 5 avp@avp-xub11:hashcode$ As you can see - Parse error in a block that is not executed. But the error response echo Ошибка2; we will see if we fix the first (missing ; )
avp@avp-xub11:hashcode$ php <t.php 2 PHP Notice: Use of undefined constant Ошибка2 - assumed 'Ошибка2' in - on line 10 Ошибка2avp@avp-xub11:hashcode$ It seems to me that now everything is quite obvious.
And nothing that this is actually different designs and you can not compare them?
exit is a construct, proof ), they cannot be measured (perhaps, of course, everything, but there it’s necessary to disassemble the core of the puff, who needs it?). In any case, it will look like an exit - collect classes, functions, constants, etc. But it will not perform. - user31688On the first link in google:
$start = microtime(true); // ВАШ СКРИПТ $time = microtime(true) - $start; printf('Скрипт выполнялся %.4F сек.', $time); if resolution of microtime not enough. (Or I’ll be completely disappointed in PHP.) - Athariif :) - user31688exit calls? - AthariSource: https://ru.stackoverflow.com/questions/413497/
All Articles