The optimization of Eval, based on the use of the Eval method to achieve inefficient problem

Source: Internet
Author: User
Demand:
To obtain the expected value according to some specified calculation formula.

Example:

Supplied data source $row = Array (            ' PV ' =        +  ,            ' uv ' =        =  6,            ' st ' = ' =  650,            ' NUV '       +  /-2);//demand formula, some quantity 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);//after processing formula $STR = "@\ $s = $gx;"; /legal PHP Statement try{    @eval ($STR);//str and eval before using @ Ensure that no errors are displayed, such as a variable does not exist, or a divisor of 0}catch (Exception $e) {}//This side result can be calculated


This method may be thought of, and there are some variations of this method:
1, using Preg_replace_callback
2,preg_replace ('//ie ') uses the E modifier, actually with 11 like
3, using SQL to execute the company, "Select formatted formula", but this method can not process no this variable, there is a divisor of 0 o'clock, will return null


These are my current methods.
Loop 1w times, 0.25 seconds or so,
If 10w times, it will be more than 2 seconds.

This efficiency is not yet acceptable.

Man, is there any good way????


Reply to discussion (solution)


All to discuss ....

First support the landlord

Still want to how fast, can't any faster. You calculate directly with data values:
Echo ($row [' PV '] + $row [' UV '])/($row [' UV ']-$row [' NUV '] + 1) *10+ $row [' NUV ']-$row [' UV '] + $row [' no ']/0+3.5;
The speed should be the fastest, but the test results are the same.

The efficiency test of the second e is no, it feels faster.

Still want to how fast, can't any faster. You calculate directly with data values:
Echo ($row [' PV '] + $row [' UV '])/($row [' UV ']-$row [' NUV '] + 1) *10+ $row [' NUV ']-$row [' UV '] + $row [' no ']/0+3.5;
The speed should be the fastest, but the test results are the same.
Don't you see the need, if the formula changes?
There are hundreds of formulas?

The efficiency test of the second e is no, it feels faster.
Basically, the E modifier is to perform the replace as Eval.

The supplied data source, which needs to be sorted first, $row the number of characters in front of the key row = Array (        ' nuv ' = +  2, '        PV '        = +  ,        ' uv '        = >  6,        ' st ' = =  650, $search  = Array_keys ($row); $replace = Array_values ($row); function Cal () {    global $search, $replace;    Demand formula, some quantity may not exist: if no    $gx = ' (PV+UV)/(uv-nuv+1) *10+nuv-uv+no/0+3.5 ';    $GX = Str_replace ($search, $replace, $GX);//Str_replace instead of preg_replace, the efficiency will be higher    $str = "@\ $s = $gx;"; /legal PHP Statement    try{        @eval ($STR);//str and eval before using @ Ensure that no errors are displayed, such as a variable does not exist, or a divisor of 0    }catch (Exception $e) {}} $t 1 = Microtime (True); for ($i = 0; $i < 10000; $i + +) {    cal ();} Echo Microtime (True)-$t 1;//output:0.18727397918701

PHP code?123456789101112131415161718192021222324252627282930313233//provides a data source that needs to be sorted first, with the number of characters in front of the key row $row = Array (' NUV ' = > 2, ' pv ' = +, ' UV ' = 6, ...

Thank you for your answer.

I have tried this method, but there will be some problems.
$GX = Str_replace ($search, $replace, $GX);

$row = Array (   ' uv ' = = '   nuv '); $gx = ' Uv+nuv+uv '; $gx = Str_replace ($search, $replace, $GX);//output: 50+n 50+50

But the main efficiency exists in eval here, Str_replace replace Preg this way, the efficiency of ascension is not ideal

10,000 times 0.25 seconds is fast enough.

There is a parsing process during eval, which is more time consuming
Time spent on regular and eval tests were tested, preg_replace accounted for more than 30% points, and Eval accounted for over 60% points
The problem is not solved from the code level
You should probably consider precompiling to program files or Phar

Reference 7 Floor USTB reply: PHP code?123456789101112131415161718192021222324252627282930313233//provides a data source that needs to be sorted first, the number of characters of the key row in front $row = Array (' NUV ' = 2, ' PV ' = +, ' UV ' ...

The first line says that it needs to be sorted first ...

Reference 8 Floor Yangball reply: Reference 7 floor USTB reply: PHP code?123456789101112131415161718192021222324252627282930313233//provides the data source, need to first sort, $row = Array (' NUV ' + 2, ' PV ' ...) with a number of characters in front of the key row.
Oh, I'm sorry, I didn't notice.
The first sort is able to solve.

10,000 times 0.25 seconds is fast enough.

There is a parsing process during eval, which is more time consuming
Time spent on regular and eval tests were tested, preg_replace accounted for more than 30% points, and Eval accounted for over 60% points
The problem is not solved from the code level
You should probably consider precompiling to program files or Phar
There is no better way out for the time being. For the time being, use this method first.
It looks like you need to write the extension to make it more reliable.

Fast enough, how fast, what bike?

  • Related Article

    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.