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.

Link to comment
Share on other sites

I have just seen microtime used a lot so I need to find out if its something I can use. Just trying to find out more about it. Can you tell me more about the exectution time scripts your talking about and how the microtime in those can help them?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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();
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.