ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

A_05 性能调优:采用BenchmarkDotNet对c#代码进行基准测试,

2021-07-28 21:00:17  阅读:178  来源: 互联网

标签:string 05 c# BenchmarkDotNet humi using local valu


1、BenchmarkDotNet

BenchmarkDotNet可帮助将方法转换为基准、跟踪其性能并共享可重复的测量实验。简而言之,采用BenchmarkDotNet可以对方法或者代码块进行基准测试,可以对代码进行调优,并且对代码进行侵入性很低。可以在同一环境下测试不同的方法性能;可以在不同平台(.net core、.net framework)测试相同的代码,达到测试不同平台的性能。

官方文档资料

2、Demo操作

(1)测试结果重要参数说明:

Method:测试的方法或代码;

Mean:测试方法平均用时;

Allocated:测试时需要分配的内存

 

 (2)代码

对lua脚本解释器库NLua和MoonSharp进行基准测试,用于比较那个性能更加优秀

using BenchmarkDotNet.Attributes;
using MoonSharp.Interpreter;
using NLua;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenchmarkTestDemo
{
    /// <summary>
    /// Lua脚本性能基准测试
    /// </summary>
    [MemoryDiagnoser]
    public class LuaScriptTestContext
    {
        [Benchmark]
        public void NLua()
        {
            Lua state = new Lua();
            string hex1 = "001100220044555";
            string scriptCode = @"
                    function operate(hexArr,executeCode)
                        if(executeCode==""read"")
                        then
                            local index=1
                            local string=string
                            temp='""temp""'
                            humi='""humi""'
                            local tmp_valu=string.sub(hexArr,3,4)
                            local humi_valu=string.sub(hexArr,7,8)
                            local result = string.format(""{%s:%d,%s:%d}"",temp,tonumber(tmp_valu,16),humi,tonumber(humi_valu,16))
                            return result
                        else
                            return """"
                        end
                    end";
            state.DoString(scriptCode);
            var scriptFunc = state["operate"] as LuaFunction;
            var res = scriptFunc.Call(hex1, "read").First();
        }

        [Benchmark]
        public void MoonSharp()
        {
            string hex1 = "001100220044555";
            string scriptCode = @"
                    function operate(hexArr,executeCode)
                        if(executeCode==""read"")
                        then
                            local index=1
                            local string=string
                            temp='""temp""'
                            humi='""humi""'
                            local tmp_valu=string.sub(hexArr,3,4)
                            local humi_valu=string.sub(hexArr,7,8)
                            local result = string.format(""{%s:%d,%s:%d}"",temp,tonumber(tmp_valu,16),humi,tonumber(humi_valu,16))
                            return result
                        else
                            return """"
                        end
                    end";

            Script script = new Script();
            script.DoString(scriptCode);
            var res = script.Call(script.Globals["operate"], hex1, "read").String;
        }
    }
}
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using System;

namespace BenchmarkTestDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Summary summary = BenchmarkRunner.Run<LuaScriptTestContext>();
            Console.Read();
        }
    }
}

(3)测试报告

 

 从测试报告可以看出,NLua解释器库性能更加优秀

 

标签:string,05,c#,BenchmarkDotNet,humi,using,local,valu
来源: https://www.cnblogs.com/hjwcore/p/15072546.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有