LoginSignup
31
32

More than 5 years have passed since last update.

通信、パースなど処理にかかる時間を計測する。

Posted at

処理にかかる時間を計測

基本的なことですが、通信にかかる時間やダウンロードをしたデータをパースするなどユーザーに極力待たせずに処理を終わらせるのが望ましいとは思います。

連続した処理などで実際どの処理で時間がかかっていたのか知らずにやっていて、調べてみたら思っていた所と違う場所で処理に時間がかかっていた、、、ということがあったので忘備録的に書いてみます。


// インスタンス変数
NSDate *startDate;
NSDate *endDate;

// 処理開始位置で現在時間を代入
startDate = [NSDate date];

// 処理終了位置で現在時間を代入
endDate = [NSDate date];

// 開始時間と終了時間の差を表示
NSTimeInterval interval = [endDate timeIntervalSinceDate:startDate];
NSLog(@"処理開始時間 = %@",[self getDateString:startDate]);
NSLog(@"処理終了時間 = %@",[self getDateString:endDate]);
NSLog(@"処理時間 = %.3f秒",interval);


// 日付をミリ秒までの表示にして文字列で返すメソッド
- (NSString*)getDateString:(NSDate*)date
{
     // 日付フォーマットオブジェクトの生成
     NSDateFormatter *dateFormatter = [NSDateFormatter new];
     // フォーマットを指定の日付フォーマットに設定
     [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss.SSS"];
     // 日付の文字列を生成
     NSString *dateString = [dateFormatter stringFromDate:date];

     return dateString;
}

NSDateを出力すると下図の様になって正確な時間まで分からない(差は正確に表示されています)
uploading image.png...

ミリ秒まで表示するメソッドを使って表示すると差が分かりやすい。
uploading image.png...

31
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
32