Jump to content

Microtime help, already looked in the manual.


11Tami

Recommended Posts

Hello, regarding microtime etc. I already saw what the manual said and all it says it that it returns the unix timestamp for a particular time. I need to know some opinions on how this could work in some php functions. For instance if I set microtime on a random function, by default does it only show the particular time on page load only? Or is there a way to keep it firing.....?? Please let me know, thank you very much.

The execution time script is a simple script that display how long it takes for a block of code to execute.  This is used frequently in development, but also frequently taken off before the site goes live.  This script can easily allow a code developer to trace the source of a slow script down to a single line or maybe a block of code.

 

PHP.net's microtime page has an example:

http://us2.php.net/manual/en/function.microtime.php

 

They are the first two examples on the page and they simple show how long it took the script to execute.

Thank you I appreciate that explanation. I uploaded them to take a look. They are just saying how long it took the script to execute and nothing else I take it, like you said.

 

Is there any way to get a script to keep executing using microtime? Or does it need a page refresh to keep executing. Please let me know someone, thank you very much.

You can do something like this (PHP 5):

 

<?php
$startTime = microtime(true);
$timeToRunFor = 1000;
$endTime = $startTime + $timeToRunFor;
while (microtime(true) < $endTime) {
    someFunctionToExecute();
}
?>

 

It will run someFunctionToExecute() repeatedly for 1000 microseconds

I appreciate that thanks for getting me started with this. Just for fun I put an echo in this part of it.. someFunctionToExecute(echo "test");

 

But it wouldn't accept php code inside someFunctionToExecute(); Don't I put a function inside the paranthesis? Any idea what I'm doing wrong? Thank you very much.

 

 

someFunctionToExecute() is a pointer to a function called someFunctionToExecute.  Since this function is undefined, it will generate errors.  Try this:

 

<?php
function someFunctionToExecute() {
     echo "test";
}

$startTime = microtime(true);
$timeToRunFor = 1000;
$endTime = $startTime + $timeToRunFor;
while (microtime(true) < $endTime) {
    someFunctionToExecute();
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.