0

I'm getting some benchmark data about page execution time. Its provided by the framework as a string. I want to convert it into a float, multiply it by 1000, and store it in the DB as an int. It seems to be acting really strange and I was hoping someone can help me figure out why. Here is the code:

    $elapsed = $this->benchmark->elapsed_time();
    var_dump("before:");
    var_dump($elapsed);

    $elapsed = floatval($elapsed);

    var_dump("after:");
    var_dump($elapsed);

Here is the result:

string 'before:' (length=7) string '0.7608' (length=14) string 'after:' (length=6) float 0

EDIT: I figured this out thanks to someone pointing out that the string length was wrong. Apparently the method is returning the string '{elapsed_time}', the framework is buffering the output, and then replacing that string with the final eval time. Thanks for the help.

6
  • 4
    0.7608 doesn't have a length of 14. That seems suspicious. Commented Apr 20, 2012 at 18:28
  • yea, good point. i have no idea why Commented Apr 20, 2012 at 18:29
  • 1
    Also, what culture is your server? i.e. could it maybe use , as a decimal separator? Commented Apr 20, 2012 at 18:29
  • and it might help to know what's in $this->benchmark (at least elapsed_time() if nothing else) Commented Apr 20, 2012 at 18:30
  • 2
    Why does a function called elapsed_time() return a string anyway? Commented Apr 20, 2012 at 18:30

5 Answers 5

2
$elapsed = ((float)$this->benchmark->elapsed_time()) * 1000;
Sign up to request clarification or add additional context in comments.

Comments

1
<?php

    $time = '0.5643';
    var_dump( $time );        
    //output  :: string(6) "0.5643"
    var_dump( $time * 1000 ); 
    //output  :: float(564.3)
?>

Everything as expected .. but then you do something really stupid like this:

<?php

    $badtime = "\0\0\0\0\0\0\0\0" . $time;
    var_dump( $badtime );        
    //output  :: string(14) "0.5643"

    var_dump($badtime * 1000); 
    //output  :: int(0)

    $goodtime = str_replace("\0", "", $badtime);
    var_dump($goodtime * 1000);
    //output  :: float(564.3)

?>

3 Comments

Thats what i thought. I have to be missing something strange. $elapsed = floor($elapsed * 1000); is returning float 0 also.
@AndyGroff , updated ... this might be the solution. Also, you might looks into some less-horrid framework =/
Thanks for the update. I figured out what was happening and updated the question. Code igniter isn't a bad framework overall, but here the documentation was hiding what was going on. I re-read this in the documentation: "CodeIgniter does not stop the benchmark until right before the final output is sent to the browser. It doesn't matter where you use the function call, the timer will continue to run until the very end." Only then i realized the function wasn't actually returning the true time elapsed data.
1

Not sure, but $elapsed *= 1000; should be fine.

1 Comment

doesnt work. there must be something strange about the string thats being returned since the length isn't correct in var dump.
1

<?php var_dump((float)'0.734' * 1000); //float(734) ?>

3 Comments

$elapsed = (float)($elapsed * 1000); returns 0
try this $elapsed = (float)$elapsed * 1000 cast $elapsed before the multiplication
I figured out the problem. The code igniter framework is doing some funky output buffering and not returning that string at all.
1

It could be that $this->benchmark->elapsed_time() is prepending a load of junk before the float, which would be why 0.7608 is 14 chars as minitech pointed out.

try:

$elapsed = floatval(trim($elapsed));

or

$elapsed = floatval(preg_replace('/[^0-9\.]/', '', $elapsed));

Bit of an odd one but the above may help, i'm not certain.

1 Comment

Thanks for the help. As it turns out, the function returns a string '{elapsed_time}', and then the actual value replaces that string in the output. The didn't document it clearly at all. Thanks, though, noticing the incorrect string length really helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.