Home  >  Article  >  Backend Development  >  Duplicate issue about microtime output in php

Duplicate issue about microtime output in php

WBOY
WBOYOriginal
2016-10-22 00:14:231041browse

Output microtime() in different places in my method and the result shows the same time Duplicate issue about microtime output in php

Server informationDuplicate issue about microtime output in php

Reply content:

Output microtime() in different places in my method and the result shows the same time Duplicate issue about microtime output in php

Server informationDuplicate issue about microtime output in php

Microtime is not unique. Who stipulates that it cannot output the same time?

Note: PHP's microtime directly uses the result of the gettimeofday function in the system for output, without any cache processing. If the results are the same, PHP will not memorize it.

Implementation of microtime in PHP 5.3.28

<code>static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
{
    zend_bool get_as_float = 0;
    struct timeval tp = {0};

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &get_as_float) == FAILURE) {
        return;
    }

    if (gettimeofday(&tp, NULL)) {
        RETURN_FALSE;
    }

    if (get_as_float) {
        RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
    }

    if (mode) {
        timelib_time_offset *offset;

        offset = timelib_get_time_zone_info(tp.tv_sec, get_timezone_info(TSRMLS_C));
                
        array_init(return_value);
        add_assoc_long(return_value, "sec", tp.tv_sec);
        add_assoc_long(return_value, "usec", tp.tv_usec);

        add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
        add_assoc_long(return_value, "dsttime", offset->is_dst);

        timelib_time_offset_dtor(offset);
    } else {
        char ret[100];

        snprintf(ret, 100, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, tp.tv_sec);
        RETURN_STRING(ret, 1);
    }
}</code>

Microseconds are like this. I remember the teacher talked about the accuracy of the PHP algorithm. It still has errors. Please ask the master downstairs to answer the specific questions

echo microtime();
sleep(1);
echo microtime();
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn