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

  • Home
  • SEARCH
  • 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 752707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:48:45+00:00 2026-05-14T14:48:45+00:00

I’m trying to determine CPU usage of my PHP scripts. I just found this

  • 0

I’m trying to determine CPU usage of my PHP scripts. I just found this article which details how to find system and user CPU usage time (Section 4).

However, when I tried out the examples, I received completely different results.

The first example:

sleep(3);

$data = getrusage();
echo "User time: ".
    ($data['ru_utime.tv_sec'] +
    $data['ru_utime.tv_usec'] / 1000000);
echo "System time: ".
    ($data['ru_stime.tv_sec'] +
    $data['ru_stime.tv_usec'] / 1000000);

Results in:

User time: 29.53
System time: 2.71

Example 2:

for($i=0;$i<10000000;$i++) {

}

// Same echo statements

Results:

User time: 16.69
System time: 2.1

Example 3:

$start = microtime(true);  
while(microtime(true) - $start < 3) {  

}  

// Same echo statements

Results:

User time: 34.94
System time: 3.14

Obviously, none of the information is correct except maybe the system time in the third example. So what am I doing wrong? I’d really like to be able to use this information, but it needs to be reliable.

I’m using Ubuntu Server 8.04 LTS (32-bit) and this is the output of php -v:

PHP 5.2.4-2ubuntu5.10 with Suhosin-Patch 0.9.6.2 (cli) (built: Jan  6 2010 22:01:14)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
  • 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-05-14T14:48:45+00:00Added an answer on May 14, 2026 at 2:48 pm

    You can use the system ‘time’ command to verify this information externally:

    /usr/bin/time php script.php
    

    which will print something like:

    0.03user 0.00system 0:03.04elapsed 0%CPU (0avgtext+0avgdata 32752maxresident)k
    0inputs+0outputs (0major+2234minor)pagefaults 0swaps
    

    Of course, dont’t forget that the getrusage() information is CPU time used and microtime() is wall clock time. The program may run for 10 minutes according to the clock on the wall, but internally may only use a few seconds of CPU time. Then there’s contending for CPU time with all the background programs running on the system, resource contention, plus regular housekeeping.

    There’s far too many factors involved to be able to get an accurate timing for such short periods. Doing three runs of the while(microtime()) version of your loop, I got the following timings:

    user: 0.98, 0.09, 0.90
    sys: 0.12, 0.05, 0.94

    Obviously quite the variance. Even just a simple <? print_r(getrusage()) ?> has utime/stimes ranging from 0 to 0.03.

    Try running your loops for longer periods, and do something within them to increase cpu usage. Right now your numbers are too small to accurately measure.

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

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ok I got this answer from somewhere... using System.Diagnostics; using… May 14, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer There are actually three segmentation faults here: fread(content,1,lsize,file_pointer); strcpy(temp,buffer); row… May 14, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer These paired functions will find the longest common string in… May 14, 2026 at 11:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.