Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:55:15+00:00 2026-06-09T08:55:15+00:00

I was looking at execution time of some functions, but i have found that

  • 0

I was looking at execution time of some functions, but i have found that microtime is getting wrong, in one microtime()

implementation №1 always first time is always getting more then second microtime() executing, when i testing one function and saw that one case is faster then another, but after place replace (2nd function to 1st place), it getting slower anyway, even if it was 3x faster…

function dotimes($times,$eval,$data=false) {for(;--$times;) eval($eval);}


$start = microtime(true);

dotimes(1000,'$i = $i?$times/2:1;');

printf(": executed : %f\n",microtime(true)-$start);

$start = microtime(true);

dotimes(1000,'$i = $i?$times/2:1;');

printf(": executed : %f\n",microtime(true)-$start);

// 1st case is always slower...

implementation №2 iv made sometimes before microtime() as static data storage, but in that case the time of execute is always second time is slower then first (oposite to implementation №1)

function get_execution_time()
{
    static $microtime_start = null;

        return $microtime_start === null ? $microtime_start = microtime(true) : microtime(true) - $microtime_start;

}


function dotimes($times,$eval,$data=false) {for(;--$times;) eval($eval);}

get_execution_time();
dotimes(1000,'$i = $i?$times/2:1;');

printf(": executed : %f\n<br>",get_execution_time());
get_execution_time();
dotimes(1000,'$i = $i?$times/2:1;');
printf(": executed : %f\n<br>",get_execution_time());
//now 2nd case is faster..
  • Can somebody tell me what is going up? Why these microtimes in one case 1st always slower, and throught static storage 2nd execute is slow down, WHY?

ps if someone need tiny mt function here is my FIXED AND WORKING CORRECT:

function get_mt() {static $mt; return $mt ? microtime(true)-$mt.$mt=null : $mt=microtime(true);}

attached :

function get_mt() {static $mt; return $mt?microtime(true)-$mt:$mt=microtime(true);}

function dotimes($times,$eval,$data=false) {for(;--$times;) eval($eval);}

$start = microtime(true);
get_mt();
dotimes(10000,'$i = $i?$times/2:1;');
printf(":clean executed : %f\n<br>",microtime(true)-$start);
printf(":static executed : %f\n<br>",get_mt());
$start = microtime(true);
get_mt();
dotimes(10000,'$i = $i?$times/2:1;');
printf(":clean executed : %f\n<br>",microtime(true)-$start);
printf(":static executed : %f\n<br>",get_mt());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:55:16+00:00Added an answer on June 9, 2026 at 8:55 am

    So far, I see that implementation №1 is correct. No clue what you tried in your second implementation.

    The advice here – never test two cases in the same script. Run them separately a few times and then find the average time. PHP allocates memory when it needs and this is a slow operation. The second case may reuse already allocated memory and skip this operation and you get wrong results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to measure the execution time taken by some functions in my
I wanted some system where I didn't have to keep looking at the Exception
So basically I have just benchmarked my update_feeds controller and found that the amount
I am looking for a weak reference implementation similar to java.lang.ref.WeakReference , but which
I am looking for some JavaScript simple samples to compute elapsed time. My scenario
I have a number of functions that parse data from files, usually returning a
I have noticed that when I run a report that contains nothing but an
I have some global variables (classes) that as I understand have created before main()
Iv wrote some C# code which seemed to be taking its sweet time, but
I'm looking for a way how to split job execution in talend studio according

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.