I'm probably overthinking this, but wanted others' input. I run a particular PHP script around 1 million times per day in the background to process incoming SNMP traps. I revise and test my PHP script from the command line and have a lot of echo() functions throughout the script in order to check the data as it's being processed when I test the script. I am wondering if I should wrap these echo() functions into a quick checksum in order to verify this script is actually being run from the command line before I echo to the screen in an effort to save cpu cycles when this script is being run in the background, or if the cpu cycles vs the checksum creates a negligible situation and it's worth just ignoring the few extra clock cycles I might be wasting by calling echo() when the process is running in the background. Thanks in advance!
Asked
Active
Viewed 83 times
0
-
2check: https://stackoverflow.com/questions/173851/what-is-the-canonical-way-to-determine-commandline-vs-http-execution-of-a-php-s – Peter M Aug 18 '17 at 17:58
-
remember that checking if the process is running in the background around every `echo` may actually take up more cycles! – Wee Zel Aug 18 '17 at 18:34
-
@WeeZel wasn't aware of php_sapi_name. Thank you. But that's what I was trying to get at: is it more expensive cpu wise to check if the proecess is running in the background vs just running echo regardless. – Chris Utter Aug 18 '17 at 18:44
-
@ChrisUtter just tested 1000000 `echo` of 64 characters with the output piped to null took 26.27 seconds on my VM, the same test but adding the test `if (php_sapi_name() === 'cli')` took 1.32 seconds – Wee Zel Aug 18 '17 at 19:09
-
@WeeZel didn't think to do that. Guess I should have. Thanks for the effort. And the insight. – Chris Utter Aug 18 '17 at 19:20