简体   繁体   English

比较/设置字符串的最快方法

[英]Fastest way to compare/set strings

I have an array of Place objects. 我有一个Place对象数组。 Each Place object has a name and code property, both strings. 每个Place对象都有一个namecode属性,均为字符串。 Each Place object already has a code , but I need to look up the name property from a server. 每个Place对象已经有一个code ,但是我需要从服务器中查找name属性。 I get back 2 arrays: one contains name, the other codes. 我得到2个数组:一个包含名称,另一个包含代码。 These arrays are ordered so that the name at some index in the nameArray corresponds exactly with the code at the same index of the codeArray . 这些阵列是有序的,这样的name在一些指标nameArray与完全对应code的相同指数codeArray

I have been looping through the array of Place objects, then checking to see if the code property for that Place is the same as the current index in the codeArray . 我一直在遍历Place对象的数组,然后检查该Placecode属性是否与codeArray的当前索引相同。 If it is, I set the name of that Place by using the same index in the nameArray : 如果是的话,我通过在nameArray使用相同的索引来设置该Placename

for(int i = 0; i < [placesArray count]; i++)
{
    for(int j = 0; j < [codeArray count]; j++) {

       if([[[placesArray objectAtIndex:i] code] isEqualToString:[codeArray objectAtIndex:j]]) {   
            [[placesArray objectAtIndex:i] setName:[nameArray objectAtIndex:j]];
       }
    }
}

This works but isn't terribly fast - it can take 30 seconds with 1000+ Places to loop through. 这行得通,但并不是很快-可能需要30秒钟才能遍历1000个以上的位置。

Is there a faster way? 有没有更快的方法?

As with anytime you're trying to optimize performance, you should profile the code using Instruments to find out where the bottleneck actually is. 与尝试优化性能的任何时候一样,您应该使用Instruments来分析代码以找出瓶颈所在。 That said, looping through the placesArray for each name in the nameArray and doing a string comparison is pretty inefficient. 就是说,遍历placeArray中的nameArray中的每个名称并进行字符串比较效率很低。

How about something like this? 这样的事情怎么样?

NSMutableDictionary *placesByCode = [NSMutableDictionary dictionaryWithCapacity:[placesArray count]];
for (Place *aPlace in placesArray) {
    [dictionary setObject:aPlace forKey:aPlace.code];
}

NSMutableDictionary *namesByCode = [NSMutableDictionary dictionaryWithCapacity:[namesArray count]];
for (int i=0; i<[namesArray count]; i++) {
    NSString *name = [namesArray objectAtIndex:i];
    NSString *code = [codeArray objectAtIndex:i];
    [namesByCode setObject:name forKey:code];
}

for (NSString *code in namesByCode) {
    Place *place = [placesByCode objectForKey:code];
    place.name = [namesByCode objectForKey:namesByCode];
}

Looking up each place by its code in the dictionary should be quite a bit faster than manually looping through the whole place array for each name. 通过字典中的代码查找每个位置应该比手动遍历每个名​​称的整个位置数组快得多。

You can use for NSArray -containsObject 您可以使用NSArray -containsObject

if ([myarray containsObject:myObject]) {
    // ...
}

Try using a break statement in the inner loop. 尝试在内循环中使用break语句。 This way you don't need to loop through the entire loop each time. 这样,您无需每次都遍历整个循环。

for(int i = 0; i < [placesArray count]; i++)
{
    for(int j = 0; j < [codeArray count]; j++) {

       if([[[placesArray objectAtIndex:i] code] isEqualToString:[codeArray objectAtIndex:j]]) {   
            [[placesArray objectAtIndex:i] setName:[nameArray objectAtIndex:j]];
            break;
       }
    }
}

You could also make the second array become smaller as you find more results. 当您找到更多结果时,也可以使第二个数组变小。 It will cost you more memory but 1000 strings isn't much anyway. 这将花费您更多的内存,但是无论如何都不会有1000个字符串。

NSMutableArray * tempCodeArray = [NSMutableArray arrayWithArray:codeArray];
for(int i = 0; i < [placesArray count]; i++)
{
    for(int j = 0; j < [tempCodeArray count]; j++) {

       if([[[placesArray objectAtIndex:i] code] isEqualToString:[tempCodeArray objectAtIndex:j]]) {   
            [[placesArray objectAtIndex:i] setName:[nameArray objectAtIndex:j]];
            [tempCodeArray removeObjectAtIndex:j];
            break;
       }
    }
}

The problem was not the counting for the array, it's the embedded for loop which will take O(n*n) and in Andrew's solution, it's only O(n)+O(n)+O(n)+whatever take to find a object of the key in the dictionary, which i guess would be in a hash table lookup and that's really fast. 问题不是数组的计数,而是嵌入的for循环,它将占用O(n * n),而在安德鲁的解决方案中,这仅是O(n)+ O(n)+ O(n)+所需要的字典中键的一个对象,我猜这将在哈希表查找中,这确实非常快。

Colby, you probably will be ok with Andrew's solution. 科尔比,您可能会同意安德鲁的解决方案。 If you still wanna improve the performance, then a good idea would be sort the array's first then do lookup. 如果您仍然想提高性能,那么最好先对数组进行排序,然后再查找。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM