The above method took on average 5.7151877 seconds, while a method using substr and simply adding strings with. I'll admit I didn't take into account martijn at vanderlee dot com's comments on testing accuracy, but as I figured the looping code etc would be the same, and this was only meant as a relative comparison, it should not be necessary. I did this 10 times to make sure there wasn't a problem of other things putting a load spike on the server. I implemented the various methods, ran each in a tight loop 1,000,000 times, and compared runtimes (and output). Of the methods I've seen here, and thought up myself, to convert microtime() output into a numerical value, the microtime_float() one shown in the documentation proper(using explode,list,float,+) is the slowest in terms of runtime. $tab_result_bench = mini_bench_to ( $t, true ) Įcho var_export ( $tab_result_bench, true ) $str_result_bench = mini_bench_to ( $t ) Įcho $str_result_bench // string return default return a resume string, or array if return_array= true : Use microtime with this (very smart) benchmark function : Return round (( $seconds + $microseconds ), 6 ) to allow a float with 6 decimal digits now the integer section ($seconds) should be small enough $microseconds = $microsecondsEnd - $this -> microsecondsStart $seconds = $timeEnd - $this -> timeStart $microsecondsEnd = $this -> microsecondsStop It uses the former way of getting microseconds. Here is a solution to easily calculate the execution time of a script without having to alter any configuration parameter. It happens because the float returned has a big integer section, what crops the decimal one leaving it with less than 6 digits. Paraphrasing the characters on Silicon Valley, "Always Float! Always Float! Always Float!"Īs previously pointed out, setting the parameter $get_as_float as true will not allow microseconds precision (what would mean 6 decimal digits). The fix is to format the microtime output to force it to always be a float: When you run this at a command line, you'll see output like this (and you'll know EXACTLY when I ran this):ġ588881291 format ( 'Y-m-d H:i:s.u' ) // This will die when format is attempted on a non-object Here is some simple code to illustrate this: If it happens to land on an exact second, it returns an integer instead. It is important to note that microtime(TRUE) does NOT always return a float (at least in PHP 5.x I have not tested 7.x). Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Errors Exceptions Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |