开发者

"Streaming" data: Keep parser open or continually create a new one?

I wanted to know what is more efficient for what I am doing:

I'm bringing in (very small - 4kb or less each) .xml files which contain GPS coordinates of a vehicle. I am then parsing them (very light parsing involved) and sending them back to the delegate.

Currently, I am using a a timer whi开发者_如何学Goch every 1 second calls the following:

-(void)refreshGPSData:(NSTimer *)theTimer{
    GPSParser *parser = [[GPSParser alloc] initWithName:@"route"];
    [parser parseRssFeed:@"http://thefeed.com/feed.xml" withDelegate:self];
    [parser release];
}

My question is, would it be more efficient to do this in a different way that isn't continuously alloc and initing the parser? Should I alloc+init only one parser and then call "parseRssFeed" every 1 second. Or should i not use a timer, but instead call "parseRssFeed" every time the parser finishes and returns data to the delegate? What is best programming practice?

Please let me know if i have provided enough information. Thank you!


This depends how often you want to use the parser. If you only parse data once a minute there is no need to store a parser in an object.
If you parse the data 10 times a second you have to allocate once and store the parser.

As the other poster (who deleted his answer because this is not a .net question) suggested I made a little performance test to show that an alloc is a really slow operation.

for (NSTimeInterval i = 0; i < 1000; i++) {
    self.label1.text = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:i]];
}

vs

for (NSTimeInterval i = 0; i < 1000; i++) {
    NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
    self.label2.text = [localDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:i]];
    [localDateFormatter release];
}

gave a result of 0.0017 seconds for the first version, and 0.28 seconds for the second version. You'll get the idea. Yes, NSDateFormatter might be one of the more complex examples of an object.

If you want to use an object like a parser or a dateformatter often I would store it somewhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜