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 8628703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:36:50+00:00 2026-06-12T08:36:50+00:00

I’m currently making a PHP timer that checks how much time a certain way

  • 0

I’m currently making a PHP timer that checks how much time a certain way of doing something takes compared to another thing that accomplishes the same, basically benchmarking.

Now, I would also like to make this tool be able to tell how much memory that specific way of doing it is taking up.

So, for more info, I’m using microtime to check the start time, do 2000 thousand loops on that code and then do another microtime with some math to check how much time it took, so what I would like to do is, outside the microtime scope, also check for memory usage.

This is my current code:

// Set the amount of loops to do, default is 2000
$loops = 2000;
$start_time = microtime(true); // Start the timer

for($i = 0; $i < $loops; $i++) {
    // Code to test
}

$total_time = microtime(true) - $start_time; // Stop the timer, and figure out the total

ob_end_flush();   // Enable output again
echo $total_time; // Echo the timer's result
?>
  • 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-12T08:36:52+00:00Added an answer on June 12, 2026 at 8:36 am

    You could use memory_get_usage (http://php.net/manual/en/function.memory-get-usage.php) when you think the process is at its peak.

    Or you could also just call it occasionally and record whatever was the highest value… or as you please.

    But this is within a process. Are you talking about a PHP process “A” checking another PHP process’ memory usage?

    If so:

    $myPID = getmypid();
    $stats = explode("\n", shell_exec('pmap $(pgrep php) | grep \'total\\|\\:\''));
    for ($i = 0; $i < count($stats); $i += 2) {
        if (strpos($stats[$i], "$myPID") === false) {
            preg_match('/\d+/', $stats[$i+1], $preRes);
            $res = $preRes[0];
        }
    }
    //if $res has a value, that value is the kilobytes of memory being used by the other PHP process
    

    This solution has a problem: if you have more than 2 php processes running total, you can’t guarantee that you will be getting the right process.

    To solve this, run first the other process, obtain its PID and then pass it as argument to this process. If you have the process’ PID you want to check, you could just do this:

    $stats = explode("\n", shell_exec('pmap $(pgrep php) | grep \'total\\|\\:\''));
    for ($i = 0; $i < count($stats); $i += 2) {
        if (strpos($stats[$i], "$otherPID") === 0) {
            preg_match('/\d+/', $stats[$i+1], $preRes);
            $res = $preRes[0];
        }
    }
    //$res contains the result you want in kilobytes
    

    You could check the memory for all processes that are not you, too:

    $myPID = getmypid();
    $stats = explode("\n", shell_exec('pmap $(pgrep php) | grep \'total\\|\\:\''));
    for ($i = 0; $i < count($stats) - 1; $i += 2) {
        if (strpos($stats[$i], "$myPID") === false) {
            preg_match('/\d+/', $stats[$i+1], $preRes);
            $res[] = $preRes[0];
        }
    }
    

    So to get the maximum memory usage, just keep a $max variable and keep checking against it.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.