无为清净楼资源网 Design By www.qnjia.com

界面如下:

利用noesis.Javascript开源组件.Net中执行javascript脚本

然后,click filter button后的UI:

利用noesis.Javascript开源组件.Net中执行javascript脚本

看,筛选出来了,代码如下(js能调C#写的UserInfo类的各个属性)

复制代码 代码如下:
public class UserInfo
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public override string ToString()
        {
            return string.Format("{0}, {1}: {2}", FirstName, LastName, Age);
        }
    }

private void btnSearch_Click(object sender, EventArgs e)
        {
            List<UserInfo> users = FilterUsers(txtFilter.Text.Trim());
            lstUsers.DataSource = users;
        }

        private List<UserInfo> FilterUsers(string jsFilter)
        {
            List<UserInfo> results = new List<UserInfo>();
            if (jsFilter.Trim().Length == 0)
                return this.users;

            try
            {
                using (JavascriptContext context = new JavascriptContext())
                {
                    foreach (UserInfo user in this.users)
                    {
                        context.SetParameter("user", user);
                        context.SetParameter("isInclude", false);
                        string js = string.Format(@"
                                                if({0})
                                                    isInclude=true;
                                                else
                                                    isInclude=false;", jsFilter);
                        context.Run(js);
                        bool isInclude = (bool)context.GetParameter("isInclude");
                        if (isInclude)
                            results.Add(user);
                    }
                }
            }
            catch
            {
                txtFilter.Focus();
                txtFilter.SelectAll();
                return this.users;
            }

            return results;
        }

我们来看看效率:

复制代码 代码如下:
private void btnTest_Click(object sender, EventArgs e)
        {
            using (JavascriptContext context = new JavascriptContext())
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                int i=1000000;
                while(i-- > 0)
                {
                    context.SetParameter("ii", 1);
                    context.SetParameter("jj", 2);
                    context.SetParameter("sum", 0);
                    string js = string.Format(@"sum=ii+jj;");
                    context.Run(js);
                    int sum = (int)context.GetParameter("sum");
                }
                sw.Stop();

                long useJS_Total = sw.ElapsedMilliseconds;
                float useJS_Average = ((float)sw.ElapsedMilliseconds) / 1000000;

                sw.Reset();
                sw.Start();
                i = 1000000;
                while (i-- > 0)
                {
                    int ii = 1;
                    int jj = 2;
                    int sum = 0;
                    sum = ii + jj;
                }
                sw.Stop();

                long nouseJS_Total = sw.ElapsedMilliseconds;
                float nouseJS_Average = ((float)sw.ElapsedMilliseconds) / 1000000;

                string msg = "";
                msg += string.Format("useJS_Total: {0}\r\n", useJS_Total);
                msg += string.Format("useJS_Average: {0}\r\n\r\n\r\n", useJS_Average);
                msg += string.Format("nouseJS_Total: {0}\r\n", nouseJS_Total);
                msg += string.Format("nouseJS_Average: {0}\r\n\r\n\r\n", nouseJS_Average);

                MessageBox.Show(msg);
            }
        }

结果(上面的是通过js引擎计算的时间、下面的是纯C#计算时间,悲剧啊):这个Noesis.Javascript只适用于性能要求不高的场合

利用noesis.Javascript开源组件.Net中执行javascript脚本

标签:
noesis,开源组件,javascript脚本

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。