猿问

为什么virtual关键字很耗时间

同一个方法,加了virtual,通过父类去调用这个方法,和不加virtual调用,会占用更多时间?

方法:

  /// <summary>
        /// 针对病种库或实验组的病例检索
        /// </summary>
        public Virtual  SearchResultViewModel SearchCaseView(int? groupId, int? sdId, string viewId, bool isAdv, string filterTree, string filterParas, string sysParamLoginUser, bool forAddingCase = false, bool forExport = false)
        { 

            var searchResultReturn =   _caseService.SearchCases(groupId, sdId, viewId, isAdv, filterTree, filterParas, sysParamLoginUser, forAddingCase, forExport);

            var result = new SearchResultViewModel(searchResultReturn.Model, searchResultReturn.IsSuccessful, searchResultReturn.FailureReason);
            result.GroupInMaxNum = searchResultReturn.GroupInMaxNum;
            result.operlogStr = searchResultReturn.operlogStr;

          
            return result;
        }

调用:

 
 public ActionResult SearchCasesBySD(int sdid, string viewId, bool isAdv, string filterTree, string filterParas)
        {

  System.Diagnostics.Stopwatch logwatch = new System.Diagnostics.Stopwatch();
            logwatch.Start();
  var caseList = _service.SearchCaseView(null, sdid, viewId, isAdv, filterTree, filterParas, AuthenticatedUserName);
            logwatch.Stop();
                caseList.operlogStr += (string.Format("--Action调用SearchCaseView,isAdv,{0},总耗时{1}毫秒,viewId,{2},结束时间{3} --\r\n", isAdv, logwatch.ElapsedMilliseconds, viewId, System.DateTime.Now.ToString()));

      }

方法 SearchCaseView加不加virtual关键词 所耗得时间相差 100毫秒左右。为什么?

浮云间
浏览 576回答 4
4回答

拉风的咖菲猫

100毫秒太夸张了,确认下你不是冷启动后的第一次运行。性能上的确会欠缺点,但这个性能上的差异基本是可以忽略的。

慕丝7291255

bushi

炎炎设计

@王庆东mas: 你这里要测这个玩意的话建议你把所有干扰项排除掉,比如那个service的调用,mock掉或者写死返回值。
随时随地看视频慕课网APP
我要回答