How can I determine the set function execution time? -Php Tutorial

Source: Internet
Author: User
Tags usleep
How can I determine the set function execution time? Function (){
//...
Return true;
}
Function B (){
//...
Return false;
}
Call ();

Imagine that if a () does not return results after 1.2 seconds, B () is immediately called ();
What are some beautiful solutions?


Reply to discussion (solution)


Don't be difficult for yourself or PHP

My solutions
1. Disadvantages of ajax timeout interpretation: time is not accurate enough. Advantage: This function can be implemented temporarily
2. Disadvantages of C language extension in PHP: difficult and cost-effective; advantages: accurate time and high efficiency
3. sit down and wait for the ox ....

Libevent can implement the timer function, but your a function should be used to execute network tasks. If yes, there must be a timeout setting.

Php itself provides such features
Pretty or not, you can judge by yourself
Not all operations are calculated in the php runtime. verify it by yourself.

Register_shutdown_function ("time_out_callback"); set_time_limit (2); // assume that a timeout occurs for 2 seconds. // The simulated timeout @ c (); // error_reporting ($ es ); // restore function time_out_callback () {if (connection_status () = 2) {B () ;}} function c () {// your possible timeout code echo _ FUNCTION __, ''; while (true) {echo '. '; // echo connection_status ()."
"; // Echo 0 usleep (100000); // breathe the cpu} function B () {echo'
'. _ FUNCTION __, ''; echo 'c timeout connection status:'. connection_status (); // echo 2, c timeout}

Php itself provides such features
Pretty or not, you can judge by yourself
Not all operations are calculated in the php runtime...
The moderator is awesome and often pretty gray. although the results are not very stable (change the usleep value for a while), php's estimation has reached the limit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.