关于 eval 的优化,基于使用eval这个方法实现效率低下的问题
需求:
根据一些指定的计算公式去获得期待值。
例子:
<br />//提供的数据源<br />$row = array(<br /> 'pv' => 50,<br /> 'uv' => 6,<br /> 'st' => 650,<br /> 'nuv' => 2<br />);<br />//需求公式,可能有些量不存在:如no<br />$gx = '(pv+uv)/(uv-nuv+1)*10+nuv-uv+no/0+3.5';<br />$gx = preg_replace('/[a-z][a-z_\d]+/i', ' $row[\'${0}\'] ', $gx);//处理过后公式<br />$str = "@\$s = $gx;";//合法php语句<br />try{<br /> @eval($str);[email protected],如某个变量不存在,或除数为0等<br />}catch(Exception $e){}<br /><br />//这个方结果可求出<br />
这个方法,可能大家都想到,还有一些这个方法的变异:
1,使用 preg_replace_callback
2,preg_replace('//ie')使用e修正符,其实与1一样
3,使用sql来执行公司,"select 格式化后的公式",但这个方法不能处理no这个变量,还有除数为0时,会返回null
//以上是我目前的方法
循环1w次,0.25秒左右,
如果10w次,就2秒多了。
这个效率目前还接受不了。
哥们还有什么好办法没????
------解决方案--------------------
先支持一下楼主
------解决方案--------------------
还想怎么快,没法再快了。你直接用数据值计算:
echo ( $row['pv'] + $row['uv'] )/( $row['uv'] - $row['nuv'] +1)*10+ $row['nuv'] - $row['uv'] + $row['no'] /0+3.5;
速度应该是最快的了吧,但测试结果也一样。
------解决方案--------------------
第二种e 的效率测试了没有,感觉会快点呢。
------解决方案--------------------
<br />//提供的数据源,需要先进行排序,将字符数多的键排前面<br />$row = array(<br /> 'nuv' => 2,<br /> 'pv' => 50,<br /> 'uv' => 6,<br /> 'st' => 650,<br />);<br /><br />$search = array_keys($row);<br />$replace = array_values($row);<br /><br />function cal()<br />{<br /> global $search, $replace;<br /><br /> //需求公式,可能有些量不存在:如no<br /> $gx = '(pv+uv)/(uv-nuv+1)*10+nuv-uv+no/0+3.5';<br /> $gx = str_replace($search, $replace, $gx);// 改用str_replace替代preg_replace,效率会高一些<br /> $str = "@\$s = $gx;";//合法php语句<br /> try{<br /> @eval($str);[email protected],如某个变量不存在,或除数为0等<br /> }catch(Exception $e){}<br />}<br /><br /><br />$t1 = microtime(true);<br />for ($i = 0; $i < 10000; $i++)<br />{<br /> cal();<br />}<br />echo microtime(true) - $t1;<br /><br />// output: 0.18727397918701<br />
------解决方案--------------------
一万次 0.25 秒已经够快了
eval 时有一个语法分析过程,比较耗时

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools
