Four Eval Test Results: Do not use Codedom to do large quantities of expressions

zhaozj2021-02-16  54

Sometimes it takes an expression operation, such as string strexpression = "- 12 * (- 2.2 7.7) - 44 * 2";

Looking online, usually use codedom, see http://www.codeproject.com/csharp/runtime_eval.asp

Simplified as: System.CodeDom.Compiler.ICodeCompiler comp = (new Microsoft.CSharp.CSharpCodeProvider () CreateCompiler ().); System.CodeDom.Compiler.CompilerParameters cp = new System.CodeDom.Compiler.CompilerParameters ();

object qswhEval2 (string Expression) {StringBuilder code = new StringBuilder (); code.Append ( "using System; / n"); code.Append ( "namespace ADOGuy {/ n"); code.Append ( "public class _Evaluator { / N "); code.append (" public object __foo () "); code.Append (" {"); code.appendformat (" Return ({0}); "{0}); code.append ("} / n "); code.Append ("}} "); System.CodeDom.Compiler.CompilerResults cr = comp.CompileAssemblyFromSource (cp, code.ToString ()); System.Reflection.Assembly a = cr.CompiledAssembly; object _Compiled = a.createInstance ("adOGuy._evaluator"); system.reflection.methodInfo mi = _compiled.gettype (). getMethod ("__ foo"); return mi.invoke (_compiled, null);}

But it feels very slow, after all, it needs real-time compilation.

So, I wrote an algorithm on my own data book: String Precede (String P, String Q) {Switch (P) {CASE " ": Case "-": return ("* / (". Indexof Q)! = - 1)? "<": ">"; case "*": case "/": return (q == "(")? "<": ">"; case "(": return (q == ")")? "=": "<") ")": Return (q == "(")? "?": ">"; case "#": return (q == "#")? "=": "<";} RETURN "?";} Double Operate (Double A, Char O, Double B) {switch (o) {case ' ': Return A B; Case ' - ': Return AB; Case' * ': Return A * B; Case' / ': Return A / B;} Return 0;

Object qswheval1 (String Expression) {/ ************* (Qiushuiwuhen 2002-12-14) ************************* / Stack Narr = New Stack (), OARR = new stack (); int J = 0; double a = 0, b = 0; string w = ""; char O; matchcollection arr = regex.matches ("" " "#", @ "(((? <= (^ | / ()) -)? / d (/. / d )? | / d)"); Oarr.push ('#'); w = Convert.TOString (Arr [J ]); while (! (W == "#" && control.tostring (OARR.PEEK ()) == "#")) {if (" - * / () #" .Indexof (w)! = - 1) {switch (precede (Oarr.Peek (). ToString (), w)) {case "<": Oarr.push (w); w = convert.toString (Arr [J ]); Break; Case "=": Oarr.pop (); w = communication.tostring (arr [j ]); break; case ">": o = convert.tochar (OARR.POP ()); b = Convert.TODOUBLE (Narr.POP ()); a = convert.todouble (Narr.POP ()); Narr.push (Operate (A, O, B)); Break; default: return "error"; Break;} } else {narr.push (w); w = convert.toString (Arr [j ]);}} return narr.pop ();} Also uses JScript's EVAL for both algorithms Microsoft.jscript.vsa.vsaEngine VE = Microsoft.jscript.vsa.vsaeng INE.CREATEENGINE (); object qswheval3 (string expression) {return microsoft.jscript.eval.jscripteValuate (Expression, VE);

Object qswheval4 (string expression) {return qswhjs.qswheval.eval (expression);

The fourth kind is required to build a JS compiled as a DLL, as follows the code IMPORT System;

Package QSWHJS {Class Qswheval {Static Function Eval (Expression): Object {Return EVAL (Expression);}}}

The test code is as follows: void Page_Load (Object O, Eventargs EA) {string strexpression = "- 12 * (- 2.2 7.7) - 44 * 2"; int i = 0, c = 100; DateTime D1, D2; cp.generateexecutable = false; cp.generateinMemory = true; d1 = datetime.now; for (i = 0; i "); D1 = DateTime.now; for (i = 0; i "); D1 = DATETIME.NOW; for (i = 0; i "); D1 = DateTime.now; for (i = 0; I ");} Test Results:

Method 1: Analysis of the expression 00: 00: 00.1702448 Method 2: Utilization CodeCom 00: 00: 23.7942144 Method 3: Using JScript VSA 00: 00: 00.1902736 Method 4: Utilizing JSC DLL 00: 00: 00.2403456

Recommended here (if you want pure CSHARP) and the third (simple code, more features)

转载请注明原文地址:https://www.9cbs.com/read-28051.html

New Post(0)