2012-03-29 82 views
1

我想運行簡單的功能分析。當一個函數開始運行時,計時器將變爲星號。當它完成時,時間被輸出到日誌:如何在運行時創建函數包裝?

-(int) myFunc { 
    [MyProfileService startTimer:@"myFuncTimer"]; 
    ... code ... 
    [MyProfileService stopTimer:@"myFuncTimer"]; 
    return result; 
} 

此溶液與生產代碼搞亂。一個更優雅的soultion將在類initialize方法靜態註冊定時器:

@implementation MyClass { 

    +(void) initialize { 
      [MyProfileService monitorFunction:@Selector(myFunc) inClass[Myclass class]]; 
    } 

爲了實現這一目標,我需要一個新的實現來取代myFunc的實施:

-(void) runProfileFunc(....) { 
    [MyProfileService startTimer:@"myFuncTimer"]; 
    id result = [MyClass performSelector:@selector(myFunc) withObject: ...]; 
    [MyProfileService stopTimer:@"myFuncTimer"]; 
    return result; 
} 

與objc /運行時我可以在運行時切換功能。 如何複製原始函數簽名並將參數傳遞給包裝器中的原始函數?

+1

之前重新發明輪子,你看着儀器? – JustSid 2012-03-29 09:36:30

+0

你這樣做是因爲:a)你想讓它運行得更快,或者b)你只是想知道測量結果? – 2012-03-29 13:12:01

+0

我想收集來自我的測試者測試者的信息,但我不希望#if無處不在。 – 2012-03-31 12:49:18

回答

相關問題