The eval optimization is inefficient based on the eval method.

Source: Internet
Author: User
For eval optimization, we need to use the eval method to achieve inefficiency:
Obtain the expected value based on some specified calculation formulas.

Example:

// The provided data source $ row = array ('pv' => 50, 'u' => 6, 'st' => 650, 'nuv' => 2 ); // demand formula. some quantities may not exist, for example, 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 $ str = "@ \ $ s = $ gx ;"; // valid php statement try {@ eval ($ str); // Run @ before str and eval to ensure that no error is displayed. if a variable does not exist, or the division is 0, etc.} catch (Exception $ e) {}// the 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 ????


Reply to discussion (solution)


To discuss ....

Support

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.

The efficiency test of the second type of e does not exist. it feels like it will be faster.

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.
You haven't seen the demand yet. what if the formula has changed?
How many hundred formulas are there?

The efficiency test of the second type of e does not exist. it feels like it will be faster.
Basically the same, the e modifier uses replace as eval for execution.

// The provided data source, which needs to be sorted first. sort the keys with multiple characters in front of $ 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, for example, no $ gx = '(pv + uv)/(uv-nuv + 1) * 10 + nuv-uv + no/0 + 3.5 '; $ gx = str_replace ($ search, $ replace, $ gx); // use str_replace instead of preg_replace, higher efficiency $ str = "@ \ $ s = $ gx;"; // valid php statement try {@ eval ($ str ); // Run @ before str and eval to ensure that no error is displayed. for example, if a variable does not exist or the divisor is 0, run catch (Exception $ e) {}}$ t1 = microtime (true); for ($ I = 0; $ I <10000; $ I ++) {cal () ;} echo microtime (true) -$ t1; // output: 0.18727397918701

PHP code? 123456789101112131415161718192021222324252627282930313233 // provides the data source, which needs to be sorted first. sort the keys with multiple characters in front of $ row = array ('nuv' => 2, 'pv' => 50, 'U' => 6 ,......

Thank you for your answer.

I have tried this method, but some problems may occur.
$ Gx = str_replace ($ search, $ replace, $ gx );

$ Row = array ('uv' => 50, 'uv' => 60); $ gx = 'UV + nuv + UUV '; $ gx = str_replace ($ search, $ replace, $ gx); // output: 50 + n50 + 50

However, the main efficiency exists in eval. the str_replace method replaces the preg, which is not very efficient.

10 thousand times, 0.25 seconds, fast enough

Eval has a syntax analysis process, which is time consuming.
The time consumed by regular expressions and eval is tested. preg_replace accounts for more than 30% points, and eval accounts for more than 60% points.
The problem cannot be solved only at the code level.
You may consider pre-compiling to a program file or phar

Reference the reply from ustb on the 7th floor: PHP code? 123456789101112131415161718192021222324252627282930313233 // provides the data source, which needs to be sorted first. sort the keys with multiple characters in front of $ row = array ('nuv' => 2, 'pv' => 50, 'U '......

In the first line, it is said that the sorting needs to be done first...

Reply to reference yangball on the eighth floor: reference the reply from ustb on the seventh floor: PHP code? 123456789101112131415161718192021222324252627282930313233 // provide the data source, which needs to be sorted first. sort the keys with multiple characters in front of $ row = array ('nuv' => 2, 'pv '......
Oh, sorry, I didn't pay attention to it.
Sorting first can solve the problem.

10 thousand times, 0.25 seconds, fast enough

Eval has a syntax analysis process, which is time consuming.
The time consumed by regular expressions and eval is tested. preg_replace accounts for more than 30% points, and eval accounts for more than 60% points.
The problem cannot be solved only at the code level.
You may consider pre-compiling to a program file or phar
There is no better way at the moment. This method is used for the moment.
It seems that it is more reliable to implement write extension.

Fast enough. how fast is it? what kind of bicycle is it!

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.