Reason analysis of the Mysql execution time is negative _mysql

Source: Internet
Author: User
Copy Code code as follows:

<?php
List ($usec, $sec) = Explode (', microtime ());
$timer = (float) $usec + (float) $sec;
mysql_query ($query, $active _db);
List ($usec, $sec) = Explode (', microtime ());
$stop = (float) $usec + (float) $sec;
$diff = $stop-$timer;
?>

$stop and $timer are the floating-point numbers that are converted, and in most cases the results of the echo $stop and Echo $timer are the same as the naked eye, but they are not stored the same in the computer, so the result is not 0, and may also be negative.

The PHP manual prompts:

Floating-point precision:
Obviously a simple decimal score like 0.1 or 0.7 cannot be converted to an internal binary format without losing a little bit of precision. This can result in confusing results: for example, Floor (0.1+0.7) *10 usually returns 7 rather than the expected 8 because the internal representation of the result is actually similar to 7.9999999999 ....

This is related to the fact that it is impossible to accurately express certain decimal points with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333. . .。

So never believe that floating-point numbers are accurate to the last one, and never compare two floating-point numbers for equality. If you do need higher precision, you should use arbitrary precision mathematical functions or GMP functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.