我目前正在编写一个 XML 解析器,它解析大量数据,有很多不同的节点(XML 不是我设计的,我无法控制内容......)
无论如何,目前下载和阅读需要非常长的时间(大约 13 秒),所以我正在寻找提高阅读效率的方法。
我写了一个函数来创建哈希值,这样程序就不再需要做很多字符串比较(只是 NSUInteger 比较),但这仍然没有降低读取的复杂性......
所以我想也许我可以创建一个 IMP 数组,这样我就可以像这样:
for(int i = 0; i < [hashValues count]; i ++)
{
if(currHash == [[hashValues objectAtIndex:i] unsignedIntValue])
{
[impArray objectAtIndex:i];
}
}
或类似的东西。
唯一的问题是我不知道如何实际调用 IMP 函数?
我读过我执行 IMP 定义的选择器
IMP tImp = [impArray objectAtIndex:i];
tImp(self, @selector(methodName));
但是,如果我无论如何都需要知道选择器的名称,那有什么意义呢?
任何人都可以帮我解决我想做的事情吗?甚至只是更多提高解析器效率的方法......
以下是我的 NSXMLParser 代表的一些摘录:来自 didStartElement
if([elementName isEqualToString:@"playingFilmData"])
{
appDelegate.arrPlayingFilms = [[NSMutableArray alloc] init];
appDelegate.arrSessionTimes_ByFilm = [[NSMutableArray alloc] init];
appDelegate.arrSessionTimes_ByCinema = [[NSMutableArray alloc] init];
[self releaseData];
return;
}
else if([elementName isEqualToString:@"film_sessions"])
{
aFilm.arrSessions = [[NSMutableArray alloc] init];
[self releaseData];
return;
}
else if([elementName isEqualToString:@"session"])
{
aSession = [[ATM_SessionObject alloc] init];
aSession.session_filmID = aFilm.film_id;
[self releaseData];
return;
}
else if([elementName isEqualToString:@"sess"])
{
aFilm.arrSessions = [[NSMutableArray alloc] init];
[self releaseData];
return;
}
else if([elementName isEqualToString:@"cin"])
{
cinID = [attributeDict objectForKey:@"id"];
[self releaseData];
return;
}
else if([elementName isEqualToString:@"s"])
{
aSession = [[ATM_SessionObject alloc] init];
aSession.session_filmID = aFilm.film_id;
aSession.session_cinemaID = cinID;
[self releaseData];
return;
}
else if([elementName isEqualToString:@"flm"])
{
aFilm = [[ATM_FilmObject alloc] init];
aFilm.film_id = [attributeDict objectForKey:@"id"];
aFilm.film_epNum = 0;
[self releaseData];
return;
}
[self releaseData];
从 didEndElement
/*
*0 = nowShowing_lastUpdate
*1 = s
*2 = tit
*3 = des
*4 = rate
*5 = dir
*6 = act
*7 = rel
*8 = flm
*/
NSUInteger numHash = [appDelegate murmerHashKey:elementName WithLegth:[elementName length] AndSeed:42];
if(currentElementValue)
{
if(numHash == [[hashValues objectAtIndex:0] unsignedIntValue])
{
appDelegate.strNowShowingUpdate = currentElementValue;
self releaseData];
return;
}
else if(numHash == [[hashValues objectAtIndex:1] unsignedIntValue])
{
[aFilm.arrSessions addObject:aSession];
[appDelegate.arrSessionTimes_ByFilm addObject:aSession];
[aSession release];
aSession = nil;
}
else if(numHash == [[hashValues objectAtIndex:2] unsignedIntValue])
{
[aFilm setValue:currentElementValue forKey:@"film_title"];
[self releaseData];
return;
}
else if(numHash == [[hashValues objectAtIndex:3] unsignedIntValue])
{
[aFilm setValue:currentElementValue forKey:@"film_description"];
[self releaseData];
return;
}
else if(numHash == [[hashValues objectAtIndex:4] unsignedIntValue])
{
[aFilm setValue:currentElementValue forKey:@"film_rating"];
[self releaseData];
return;
}
else if(numHash == [[hashValues objectAtIndex:5] unsignedIntValue])
{
[aFilm setValue:currentElementValue forKey:@"film_directors"];
[self releaseData];
return;
}
else if(numHash == [[hashValues objectAtIndex:6] unsignedIntValue])
{
[aFilm setValue:currentElementValue forKey:@"film_actors"];
[self releaseData];
return;
}
}
if(numHash == [[hashValues objectAtIndex:8] unsignedIntValue])
{
[appDelegate.arrPlayingFilms addObject:aFilm];
[aFilm release];
aFilm = nil;
[self releaseData];
return;
}
[self releaseData];
我希望这有助于更多地了解我做错了什么。就像我说的,我是这个编程领域的新手(实际上,我实际上是一名数学家,而不是受过培训的程序员......),所以我真的非常热衷于学习而不是做什么!