Problems related to the efficiency of eval optimization based on the eval method

Source: Internet
Author: User
For eval optimization, we need to use the eval method to achieve low efficiency: & nbsp; obtain the expected value based on some specified calculation formulas. Example: & nbsp; // The data source $ row & nbsp ;=& nbsp; array (& nbsp; for eval optimization, inefficient implementation based on the eval method
Requirements:
Obtain the expected value based on some specified calculation formulas.

Example:

 

// Provided data source
$ Row = array (
'Pv' => 50,
'U' => 6,
'St' => 650,
'Uv' => 2
);
// Demand formula. some quantities may not exist: such as no
$ Gx = '(pv + uv)/(uv-nuv + 1) * 10 + nuv-uv + no/0 + 3.5 ';
$ Gx = preg_replace ('/[a-z] [a-z _ \ d] +/I', '$ row [\' $ {0} \ ']', $ gx); // formula after processing
$ Str = "@ \ $ s = $ gx;"; // valid php statement
Try {
@ Eval ($ str); // Run @ before str and eval to ensure that no error is displayed, for example, a variable does not exist or the divisor is 0.
} Catch (Exception $ e ){}

// This result can be obtained.


There are also some variations in this method:
1. use preg_replace_callback
2. preg_replace ('// ie') uses the e modifier, which is actually the same as 1.
3. use SQL to execute the company, "select formatted formula", but this method cannot handle the no variable. if the divisor is 0, null is returned.


// The above is my current method
1 million cycles, about 0.25 seconds,
If it is 10 million times, it will take more than 2 seconds.

This efficiency is not acceptable yet.

Is there any good way, buddy ????

------ Solution --------------------
Support
------ Solution --------------------
I still want to know how fast it is. You directly use the data value calculation:
Echo ($ row ['pv'] + $ row ['uv'])/($ row ['uv']-$ row ['uv'] + 1) * 10 + $ row ['uv']-$ row ['uv'] + $ row ['no']/0 + 3.5;
The speed should be the fastest, but the test results are the same.
------ Solution --------------------
The efficiency test of the second type of e does not exist. it feels like it will be faster.
------ Solution --------------------

// The provided data source, which needs to be sorted first, and the keys with many characters are listed first
$ Row = array (
'Nuv' => 2,
'Pv' => 50,
'U' => 6,
'St' => 650,
);

$ Search = array_keys ($ row );
$ Replace = array_values ($ row );

Function cal ()
{
Global $ search, $ replace;

// Demand formula. some quantities may not exist: such as no
$ Gx = '(pv + uv)/(uv-nuv + 1) * 10 + nuv-uv + no/0 + 3.5 ';
$ Gx = str_replace ($ search, $ replace, $ gx); // replace preg_replace with str_replace, which is more efficient.
$ Str = "@ \ $ s = $ gx;"; // valid php statement
Try {
@ Eval ($ str); // Run @ before str and eval to ensure that no error is displayed, for example, a variable does not exist or the divisor is 0.
} Catch (Exception $ e ){}
}


$ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Cal ();
}
Echo microtime (true)-$ t1;

// Output: 0.18727397918701

------ Solution --------------------
10 thousand times, 0.25 seconds, fast enough

Eval has a syntax analysis process, which is time consuming.

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.