简体   繁体   English

iPhone-遍历SQLite数据库花费的时间太长

[英]iPhone - looping through SQLite database taking too long

Hi folks hope you can help. 大家好,希望您能提供帮助。 I'm attempting to create an anagram checker for an iPhone application. 我正在尝试为iPhone应用程序创建一个字谜检查器。 What I want to do is to be able to take a long string of maximum 81 letters and then check this against a word list to find all possible permutations of any length. 我想要做的是能够容纳最多81个字母的长字符串,然后对照单词列表进行检查,以找到任何长度的所有可能排列。

I've figured out how to do this on the simulator, but when I run this on the iPhone it is extremely slow, taking around 90 seconds to loop through the entire db (about 110000 rows). 我已经找到了如何在模拟器上执行此操作的方法,但是当我在iPhone上执行此操作时,它的运行速度非常慢,大约需要90秒才能遍历整个数据库(大约110000行)。 I checked Instruments and it doesn't show any memory leaks. 我检查了乐器,它没有显示任何内存泄漏。 However when I use Object Allocations it is clear that running the query creates a massive allocation for CFString that drains everything else. 但是,当我使用对象分配时,很明显,运行查询会为CFString创建大量分配,从而耗尽所有其他内容。 This immediately runs up 3.09 MB under the overall bytes column. 这将立即在“总字节数”列下运行3.09 MB。 (In the code below, I've stripped out all of the anagram checking code as I wanted to identify what was causing the problem. So all this does at present is loop through the db without any output). (在下面的代码中,我剥离了所有的字谜检查代码,因为我想确定是什么引起了问题。因此,目前所有这些操作是通过db循环而没有任何输出)。

//create query
    NSString *querySQL2 = @"SELECT name FROM table ";

    sqlite3_stmt *statement2;
    const char *query_stmt2 = [querySQL2 UTF8String];



    sqlite3_prepare_v2(contactDB, query_stmt2, -1, &statement2, NULL);


    //loop through all rows of database
    while (sqlite3_step(statement2) == SQLITE_ROW)
    {
        NSString *laststring = [[NSString alloc] initWithUTF8String:
                          (const char *) sqlite3_column_text(statement2, 0)];



        [laststring release];
        }


    sqlite3_finalize(statement2);
   sqlite3_close(contactDB);

    }

It seems obvious to me that the creation of 'laststring' below is what is sucking up all the memory. 对我来说,显而易见的是,下面“ laststring”的创建正在消耗所有内存。 So why is it that when I put [laststring release]; 那为什么当我放[laststring release]时却如此? at the end of the while loop it appears to have no effect? 在while循环结束时似乎没有效果? I've run this code with and without releasing and the same quantity of memory is used up. 我已经运行了此代码,并且没有释放,并且消耗了相同数量的内存。 I've also tried wrapping an autorelease around it and this also had no effect. 我也尝试过在其周围包装自动发行版,但这也没有任何效果。

I've read several other queries on looping through SQLite. 我已经阅读了有关通过SQLite循环的其他一些查询。 Some of them suggested indexing but I'm not sure this will save me significant amounts of time with this problem. 他们中的一些建议建立索引,但是我不确定这会为我节省大量时间。 Also if I am searching all possible permutations from a large string of 81 letters, I'm guessing that at least 50% of the word list will need to be checked through anyway. 另外,如果我要从一个由81个字母组成的大字符串中搜索所有可能的排列,我想无论如何至少需要检查单词列表的50%。

Any suggestions on how to keep CFString down? 关于如何降低CFString的任何建议? Many thanks Dave 非常感谢Dave

Why do you alloc the NSString inside the while() loop? 为什么要在while()循环内分配NSString?

while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
      {
      NSString *laststring = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement2, 0)];    
      // do something with laststring
      }

would suffice wouldn't it? 就足够了吗?

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

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