
我试图在Android和iOS中为项目执行sqlite性能之间的基准测试,并且与AndroID相比,iOS平台上的性能似乎非常糟糕.
我想要实现的是测量将多个行(5000)插入sqlite DB并在平台之间进行比较的时间.对于AndroID,我得到大约500ms的结果来执行所有5000次插入,但对于iOS,相同的 *** 作需要20秒以上.怎么会这样?
这是我的iOS代码片段(插入部分),dataArray是一个包含5000个随机100个字符Nsstrings的数组:
int numEntrIEs = 5000;self.dataArray = [[NSMutableArray alloc] initWithCapacity:numEntrIEs];//Array for random data to write to database//generate random data (100 char strings)for (int i=0; i<numEntrIEs; i++) { [self.dataArray addobject:[self genRandStringLength:100]];}// Get the documents directoryNSArray *dirPaths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory, NSUserDomainMask, YES);Nsstring *docsDir = [dirPaths objectAtIndex:0];// Build the path to the database fileNsstring *databasePath = [[Nsstring alloc] initWithString:[docsDir stringByAppendingPathComponent: @"benchmark.db"]];Nsstring *resultHolder = @"";//Try to open DB, if file not present, create itif (sqlite3_open([databasePath UTF8String], &db) == sqlITE_OK){ sql = @"CREATE table IF NOT EXISTS BENCHMARK(ID INTEGER PRIMARY KEY autoINCREMENT, TESTColUMN TEXT)"; //Create table if (sqlite3_exec(db, [sql UTF8String], NulL, NulL, NulL) == sqlITE_OK){ NSLog(@"DB created"); }else{ NSLog(@"Failed to create DB"); } //START: INSERT BENCHMARK NSDate *startTime = [[NSDate alloc] init];//Get timestamp for insert-timer //Insert values in DB, one by one for (int i = 0; i<numEntrIEs; i++) { sql = [Nsstring stringWithFormat:@"INSERT INTO BENCHMARK (TESTColUMN) VALUES('%@')",[self.dataArray objectAtIndex:i]]; if (sqlite3_exec(db, [sql UTF8String], NulL, NulL, NulL) == sqlITE_OK){ //Insert successful } } //Append time consumption to display string resultHolder = [resultHolder stringByAppendingString:[Nsstring stringWithFormat:@"5000 insert ops took %f sec\n", [startTime timeIntervalSinceNow]]]; //END: INSERT BENCHMARKAndroID代码段:
// SETUP long startTime, finishTime; // Get database object BenchmarkOpenHelper databaseHelper = new BenchmarkOpenHelper(getApplicationContext()); sqliteDatabase database = databaseHelper.getWritableDatabase(); // Generate array containing random data int rows = 5000; String[] rowData = new String[rows]; int dataLength = 100; for (int i=0; i<rows; i++) { rowData[i] = generaterandomString(dataLength); } // FirsT TEST: Insertion startTime = System.currentTimeMillis(); for(int i=0; i<rows; i++) { database.rawquery("INSERT INTO BENCHMARK (TESTColUMN) VALUES(?)", new String[] {rowData[i]}); } finishTime = System.currentTimeMillis(); result += "Insertion test took: " + String.valueOf(finishTime-startTime) + "ms \n"; // END FirsT TEST解决方法:
您需要使用事务 – 首先执行BEGIN并使用COMMIT结束.
这应该会大大提高INSERT的性能.
http://www.titaniumdevelopment.com.au/blog/2012/01/27/10x-faster-inserts-in-sqlite-using-begin-commit-in-appcelerator-titanium-mobile/
一旦完成,我预计在两个平台上都会有5000个插件非常快.
这是另一个StackOverflow答案,其中列出了大量可以提高sqlite性能的不同内容,包括使用绑定变量和启用各种PRAGMA模式,这些模式可以提高速度的稳健性:Improve INSERT-per-second performance of SQLite?
总结以上是内存溢出为你收集整理的Android和iOS上的SQLite之间的性能差异全部内容,希望文章能够帮你解决Android和iOS上的SQLite之间的性能差异所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)