欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何獲取C#中方法的執(zhí)行時間以及其代碼注入詳解

 更新時間:2018年11月09日 14:22:56   作者:Minotauros  
這篇文章主要給大家介紹了關(guān)于如何獲取C#中方法的執(zhí)行時間以及其代碼注入的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧

前言

在優(yōu)化C#代碼或?qū)Ρ饶承〢PI的效率時,通常需要測試某個方法的運(yùn)行時間,可以通過DateTime來統(tǒng)計指定方法的執(zhí)行時間,也可以使用命名空間System.Diagnostics中封裝了高精度計時器QueryPerformanceCounter方法的Stopwatch類來統(tǒng)計指定方法的執(zhí)行時間:

1.使用DateTime方法:

DateTime dateTime = DateTime.Now;

MyFunc();

Console.WriteLine((DateTime.Now - dateTime).TotalMilliseconds);

2.使用Stopwatch方式:

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
MyFunc();

stopwatch.Stop();

Console.WriteLine(stopwatch.ElapsedMilliseconds); //本次MyFunc()方法的運(yùn)行毫秒數(shù)

//重置計時器
stopwatch.Restart(); //此處可以使用stopwatch.Reset(); stopwatch.Start();組合代替

MyFunc();

stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds); //本次MyFunc()方法的運(yùn)行毫秒數(shù)

以上兩種辦法都可以達(dá)到獲取方法執(zhí)行時間的目的,但是在需要對整個項目中的方法都進(jìn)行監(jiān)測用時時,除了使用性能分析工具,我們還可以通過代碼注入的方式給程序集中每一個方法加入計時器;

通過命名空間System.Reflection.Emit中的類可以動態(tài)的創(chuàng)建程序集、類型和成員,通常類庫Mono.Cecil可以動態(tài)讀取并修改已經(jīng)生成的IL文件,這種在不修改源代碼的情況下給程序集動態(tài)添加功能的技術(shù)稱為面向切面編程(AOP);

這里給出了一個注入使用Stopwatch來檢測方法執(zhí)行時間的代碼,這里的Mono.Cecil類庫可以通過nuget進(jìn)行安裝:

using System;
using System.IO;
using System.Linq;
using System.Diagnostics;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
static void Main(string[] args)
 {
 for (int i = 0; i < args.Length; i++)
 {
 FileStream fileStream = new FileStream(args[i], FileMode.Open);
 if (fileStream != null)
 {
 AssemblyDefinition aD = AssemblyDefinition.ReadAssembly(fileStream);
 ModuleDefinition mD = aD.MainModule;
 Collection<TypeDefinition> typeDefinition = mD.Types;
 foreach (TypeDefinition type in typeDefinition)
 {
  if (type.IsClass)
  {
  foreach (MethodDefinition method in type.Methods)
  {
  if (method.IsPublic && !method.IsConstructor)
  {
  ILProcessor il = method.Body.GetILProcessor();
  TypeReference stT = mD.ImportReference(typeof(Stopwatch));
  VariableDefinition stV = new VariableDefinition(stT);
  method.Body.Variables.Add(stV);
  Instruction first = method.Body.Instructions.First();
  il.InsertBefore(first, il.Create(OpCodes.Newobj,                       mD.ImportReference(typeof(Stopwatch).GetConstructor(new Type[] { }))));
  il.InsertBefore(first, il.Create(OpCodes.Stloc_S, stV));
  il.InsertBefore(first, il.Create(OpCodes.Ldloc_S, stV));
  il.InsertBefore(first, il.Create(OpCodes.Callvirt,                      mD.ImportReference(typeof(Stopwatch).GetMethod("Start"))));

  Instruction @return = method.Body.Instructions.Last();
  il.InsertBefore(@return, il.Create(OpCodes.Ldloc_S, stV));
  il.InsertBefore(@return, il.Create(OpCodes.Callvirt,                       mD.ImportReference(typeof(Stopwatch).GetMethod("Stop"))));

  il.InsertBefore(@return, il.Create(OpCodes.Ldstr, $"{method.FullName} run time: "));
  il.InsertBefore(@return, il.Create(OpCodes.Ldloc_S, stV));
  il.InsertBefore(@return, il.Create(OpCodes.Callvirt,                       mD.ImportReference(typeof(Stopwatch).GetMethod("get_ElapsedMilliseconds"))));
  il.InsertBefore(@return, il.Create(OpCodes.Box, mD.ImportReference(typeof(long))));
  il.InsertBefore(@return, il.Create(OpCodes.Call,                       mD.ImportReference(typeof(string).GetMethod("Concat", new Type[] { typeof(object), typeof(object) }))));
  il.InsertBefore(@return, il.Create(OpCodes.Call,                       mD.ImportReference(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }))));
  }
  }
  }
 }
 FileInfo fileInfo = new FileInfo(args[i]);
 string fileName = fileInfo.Name;
 int pointIndex = fileName.LastIndexOf('.');
 string frontName = fileName.Substring(0, pointIndex);
 string backName = fileName.Substring(pointIndex, fileName.Length - pointIndex);
 string writeFilePath = Path.Combine(fileInfo.Directory.FullName, frontName + "_inject" + backName);
 aD.Write(writeFilePath);
 Console.WriteLine($"Success! Output path: {writeFilePath}");
 fileStream.Dispose();
 }
 }
 Console.Read();
 }

完整的項目傳到了Github上=>InjectionStopwatchCode (本地下載),下載項目后,通過dotnet build命令即可編譯出可執(zhí)行程序,將目標(biāo)程序集文件拖入到該應(yīng)用程序即可在程序集目錄導(dǎo)出注入代碼后的程序集文件,經(jīng)過測試,包括方法擁有返回值和方法的參數(shù)列表中包含out和ref參數(shù)等情況都不會對運(yùn)行結(jié)果產(chǎn)生影響;

示例:

using System;

public class MyClass
{
 public void MyFunc()
 {
 int num = 1;
 for (int i = 0; i < int.MaxValue; i++)
 {
 num++;
 }
 }
}
public class Program
{
 public static void Main(string[] args)
 {
 MyClass myObj = new MyClass();
 myObj.MyFunc();
 Console.Read();
 }
}

原始IL代碼:

代碼注入后IL代碼:

代碼注入后運(yùn)行結(jié)果:

 

 總結(jié):

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • C#操作windows系統(tǒng)進(jìn)程的方法

    C#操作windows系統(tǒng)進(jìn)程的方法

    這篇文章主要介紹了C#操作windows系統(tǒng)進(jìn)程的方法,涉及C#針對windows操作系統(tǒng)進(jìn)程的創(chuàng)建與關(guān)閉的技巧,需要的朋友可以參考下
    2015-04-04
  • C#多線程死鎖介紹與案例代碼

    C#多線程死鎖介紹與案例代碼

    這篇文章介紹了C#多線程的死鎖,并使用案例代碼實現(xiàn)解決方案,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 使用遞歸實現(xiàn)數(shù)組求和示例分享

    使用遞歸實現(xiàn)數(shù)組求和示例分享

    這篇文章主要介紹了使用遞歸實現(xiàn)數(shù)組求和示例,思路是給定一個含有n個元素的整型數(shù)組a,求a中所有元素的和,需要的朋友可以參考下
    2014-03-03
  • C#?Winform實現(xiàn)進(jìn)度條顯示

    C#?Winform實現(xiàn)進(jìn)度條顯示

    這篇文章主要為大家詳細(xì)介紹了C#?Winform實現(xiàn)進(jìn)度條顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 分享一個C#編寫簡單的聊天程序(詳細(xì)介紹)

    分享一個C#編寫簡單的聊天程序(詳細(xì)介紹)

    這是一篇基于Socket進(jìn)行網(wǎng)絡(luò)編程的入門文章,我對于網(wǎng)絡(luò)編程的學(xué)習(xí)并不夠深入,這篇文章是對于自己知識的一個鞏固,同時希望能為初學(xué)的朋友提供一點參考。文章大體分為四個部分:程序的分析與設(shè)計、C#網(wǎng)絡(luò)編程基礎(chǔ)(篇外篇)、聊天程序的實現(xiàn)模式、程序?qū)崿F(xiàn)
    2015-12-12
  • C# MVC模式下商品抽獎功能實現(xiàn)

    C# MVC模式下商品抽獎功能實現(xiàn)

    這篇文章主要為大家分享了C#在MVC模式下實現(xiàn)商品抽獎功能,思路清晰,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • c# 委托的本質(zhì)是什么

    c# 委托的本質(zhì)是什么

    這篇文章主要介紹了c# 委托的本質(zhì)是什么,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#操作EXCEL DataTable轉(zhuǎn)換的實例代碼

    C#操作EXCEL DataTable轉(zhuǎn)換的實例代碼

    C#操作EXCEL DataTable轉(zhuǎn)換的實例代碼,需要的朋友可以參考一下
    2013-04-04
  • C# WebApi 路由機(jī)制剖析

    C# WebApi 路由機(jī)制剖析

    這篇文章主要介紹了C# WebApi 路由機(jī)制剖析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • C#中Params的用法

    C#中Params的用法

    這篇文章主要介紹了C#中Params的用法,以實例的形式展示了采用Params在不知道參數(shù)的數(shù)量時的應(yīng)用技巧,非常具有實用價值,需要的朋友可以參考下
    2014-11-11

最新評論