0

I'm trying to compare two functions using microtime().

However, I'm getting unexpected results.

According to my understanding of this SO post (Tracking the script execution time in PHP) I should be able to subtract $before from $after to get my execution time in seconds.

I've set my script out like this:

<?php
    $int = 4;

    $before = microtime(true);

    print_r(filter_var($int, FILTER_VALIDATE_INT));
    echo '<br />';

    $after = microtime(true);

    echo ($after - $before). 'sec' ."\n";
    echo '<br />';

    $int = 4;

    $before = microtime(true);

    print_r(is_int($int));
    echo '<br />';

    $after = microtime(true);

    echo ($after - $before) .'sec' ."\n";

I know it's not an accurate iteration of execution time as it doesn't loop the function X times to get an average - just doing a basic test at first, so please ignore that aspect.

When I go to this page in my browser, I get this output:

4

9.0599060058594E-6sec

1

9.5367431640625E-7sec

The page loads in under a second - so why 9.XYZ is appearing is getting me a little confused.

Am I using microtime() correctly? Side question: How do I see which function is the fastest? Accepted answer in linked question outputted 0ms for both functions.

edit

I changed my script to this:

<?php
    function checkInt($int)
    {
        return is_int($int);
    }

    function checkIntFilter($int)
    {
        return filter_var($int, FILTER_VALIDATE_INT);
    }

    $before = microtime(true);

    for ($i = 1; $i < 1000; $i++)
    {
        checkInt(4);
    }

    $after = microtime(true);

    echo ($after - $before). ' sec' .'<br />';

    $before = microtime(true);

    for ($i = 1; $i < 1000; $i++)
    {
        checkIntFilter(4);
    }

    $after = microtime(true);

    echo ($after - $before). ' sec';

which now outputs:

7.1048736572266E-5 sec
0.00024008750915527 sec

however, my page definitely loaded in under 7 seconds - the second result looks correct, but I'm not sure about the first ...

Community
  • 1
  • 1
treyBake
  • 6,440
  • 6
  • 26
  • 57

1 Answers1

2

You are not reading the results correctly. That's "scientific notation", which is used to express values that are either too small or to big to be handled conveniently.

7.1048736572266E-5 seconds is equivalent to 0.000071048736572266 seconds.

So you are right, your test does take significantly less time than 7 seconds.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • huh weird -> there's a heavily-upvoted comment in the php.net page for microtime (https://secure.php.net/manual/en/function.microtime.php) that says this: `Note that the timestamp returned is "with microseconds", not "in microseconds". This is especially good to know if you pass 'true' as the parameter and then calculate the difference between two float values -- the result is already in seconds; it doesn't need to be divided by a million.` -> to me that read as the result displays in seconds - but I suppose because it's got so many units passed the decimal point it shortens it using `E-` – treyBake Oct 19 '18 at 09:42
  • Yes, the return value is in seconds with microseconds, not "in microseconds". That doesn't contradict my answer at all. – yivi Oct 19 '18 at 09:43
  • I was more implying I expected result to show in seconds as in `0.ETC.` rather than use scientific notation (not really familiar with using it, so didn't expect to see it as such), so I read it in literal 7.XYZ seconds rather than the proper output – treyBake Oct 19 '18 at 09:44