好評販売中!!
2010-03-24 (Wed)
■ autorelease は遅いか速いか 
以下を見て、ちょっと気になったので自分でも検証してみた。
autorelease の方は、開放してないから速いんじゃないかなあと思ったので、以下のように。
#import <Foundation/Foundation.h> // Inteface @interface Man : NSObject { @private NSUInteger age_; } @property (nonatomic, assign) NSUInteger age; @end // Implementation @implementation Man @synthesize age = age_; @end void test_release() { NSDate* start = [NSDate date]; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; for ( int i = 0; i < 100; ++i ){ for ( int l = 0; l < 1000; ++l ){ Man* hoge = [[Man alloc] init]; hoge.age = l * i; [hoge release]; } } [pool drain]; NSLog( @"%f", [start timeIntervalSinceNow] * -1 ); } void test_autorelease() { NSDate* start = [NSDate date]; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; for ( int i = 0; i < 100; ++i ){ for ( int l = 0; l < 1000; ++l ){ Man* hoge = [[[Man alloc] init] autorelease]; hoge.age = l * i; } } [pool drain]; NSLog( @"%f", [start timeIntervalSinceNow] * -1 ); } int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; test_release(); test_autorelease(); [pool drain]; return 0; }
結果。
| release | autorelease | |
|---|---|---|
| 1回目 | 0.042711 | 0.068670 |
| 2回目 | 0.041954 | 0.066905 |
| 3回目 | 0.040351 | 0.066315 |
AutoreleasePool を作らなかった場合は tokorom さんの所と同じように autorelease の方が速い。
| release | autorelease | |
|---|---|---|
| 1回目 | 0.040539 | 0.034647 |
| 2回目 | 0.041591 | 0.034622 |
| 3回目 | 0.040015 | 0.034726 |
autorelease の開放タイミングって、メインなスレッドの処理が終わった後?でしたっけ。この結果を見る感じ、autorelease を多用してると、開放の瞬間に負荷が集中しちゃうような気がするんですけど、どうなんでしょうか。詳しい人。
トラックバック - http://iphone-dev.g.hatena.ne.jp/ktakayama/20100324
ぼくもAutoreleasePoolを含めて実測してそちらのほうが遅くなっているのを確認しました。
AutoreleasePoolでreleaseを発動する頻度をあげると極端に遅くなるので、今のところAutoreleasePoolの作成と解放に時間がかかっているように思えます。
ここからはまだ未計測なのですが、都度releaseよりも、まとめてrelease(autorelease)のほうが、解放処理自体にかかる時間は短くなるような気がします。そういった意味で、autoreleaseが遅くなるのは、paellaさんからコメントいただいた「オブジェクトが大量にプールされているときに、イベントごとに数え上げの対象になってしまい、数え上げの処理が遅くなってしまう」の部分くらいしか思い当たらないのが現状です。そのあたり、突っ込んで調査してみたいなと思ってます。
なんだか難しくなってきたので、皆さんの活躍に期待します
ひとまず元記事のほうに、都度releaseとまとめてreleaseの実測結果だけ追記させていただきましたー。