<p>I am trying to get some accurate runtime comparisons of PHP vs Python (and potentially any other language that I have to include). Timing within a script is not my problem but timing within a script does not account for everything from the moment the request is made to run the script to output.</p>

<blockquote>
  <p>1) Is it actually worth taking such things into account?</p>
  
  <p>2) Assuming it is worth taking it into account, how do I do this?</p>
</blockquote>

<p>I'm using a Mac so I've got access to Linux commands and I'm not afraid to compile/create a command to help me, I just don't know how to write such a command.</p>

[{'answer_id': 62094, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 3, 'selected': True, 'text': "<p>If your idea is to compare the languages, I'd say anything outside them is not relevant for comparison purposes. </p>\n\n<p>Nonetheless you can use the time command to measure everything and can compare it with the timing within a script.</p>\n\n<p>Like this:</p>\n\n<pre><code>$ time script.php\nHI!\n\nreal    0m3.218s\nuser    0m0.080s\nsys     0m0.064s\n</code></pre>\n\n<p>It will give you clock time, user time (php interpreter) and sys time (OS time)</p>\n\n<p>If you are thinking web, then it gets a lot harder because you would be mixing webserver overhead and that is not always easy to compare if, say, you are using WSGI v/s mod_php. Then you'd have to hook probes into the webserving parts of the chain as well</p>\n"}, {'answer_id': 62097, 'author': 'Loren Segal', 'author_id': 6436, 'author_profile': 'https://Stackoverflow.com/users/6436', 'pm_score': 1, 'selected': False, 'text': '<ol>\n<li><p>It\'s worth taking speed into account if you\'re optimizing code. You should generally know why you\'re optimizing code (as in: a specific task in your existing codebase is taking too long, not "I heard PHP is slower than Python"). It\'s <em>not</em> worth taking speed into account if you don\'t actually plan on switching languages. Just because one tiny module does something slightly faster doesn\'t mean rewriting your app in another language is a good idea. There are many other factors to choosing a language besides speed.</p></li>\n<li><p>You benchmark, of course. Run the two codebases multiple times and compare the timing. You can use the <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?time" rel="nofollow noreferrer">time</a> command if both scripts are executable from the shell, or use respective benchmarking functionality from each language; the latter case depends heavily on the actual language, naturally.</p></li>\n</ol>\n'}, {'answer_id': 62099, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': '<p>Well, you can use the "time" command to help:</p>\n\n<pre><code>you@yourmachine:~$ time echo "hello world"\nhello world\n\nreal    0m0.000s\nuser    0m0.000s\nsys 0m0.000s\nyou@yourmachine:~$ \n</code></pre>\n\n<p>And this will get around timing outside of the environment.</p>\n\n<p>As for whether you need to actually time that extra work... that entirely depends on what you are doing.  I assume this is for some kind of web application of some sort, so it depends on how the framework you use actually works... does it cache some kind of compiled (or parsed) version of the script?  If so, then startup time will be totally irrelevant (since the first hit will be the only one that startup time exists in).</p>\n\n<p>Also, make sure to run your tests in a loop so you can discount the first run (and include the cost on the first run in your report if you want).  I have done some tests in Java, and the first run is always slowest due to the JIT doing its job (and the same sort of hit may exist in PHP, Python and any other languages you try).</p>\n'}]
['https://Stackoverflow.com/questions/62079', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1384652/']