I.ndigo Blog.
Core Data over SQLite Performance Tests – Part 3
Following the last post, Core Data over SQLite Performance Tests – Part 2, when we began performance tests with Core Data, now we continue with the results of this analysis.
As we defined before, this test will show the performance of 4 situations (see details on the previous post):
- Insert without join tables;
- Inserts with tables;
- Select without join tables;
- Select with join tables.
The previous post showed the first 2 situations. This time we will cover the last 2 (selects).
1. Select without join tables
This test tries to execute selects without joins in 2 ways:
- Fetch by object’s attributes;
- Fetch by identifier.
In each case, we see how the time to execute the select varies with the number of registries of the table (from 1 to 10000):
a) Fetch by object’s attributes

| min | 0.007469 s |
| max | 0.259504 s |
| average | 0.049227 s |
| total | 492.3 s |
As we can see on the chart, when fetching by an attribute that is not indexed the time needed to execute the select varies almost linearly with the number of registries of the table. So, it is easy to think on how the performance of your table is getting worst with the time.
b) Fetch by identifier

| min | 0.000070 s |
| max | 0.004420 s |
| average | 0.000086 s |
| total | 0.8597 s |
This case shown us that fetching by the identifier (indexed), the time to fetch almost do not change with the number of registries, once the average time to fetch was almost equal the min time.
Conclusion
This tests resulted on the following table:
| Test | Average Time per Select | Total Time |
| Fetch by object’s attributes | t1 or 0.049227 s | t2 or 492.3 s |
| Fetch by identifier | 0.0017 x t1 or 0.000086 s | 0.0017 x t2 or 0.8597 s |
As we can see, for simple selects (without joins) when possible we should use identifiers to fetch, but, if we need to fetch by an attribute, it’s not hard to think about the performance, once it increases linearly with the size of the table.
2. Selects with join tables
This test shows how the time to execute a select increases as the number of join tables (in each select) and number of rows increases. The number of joins varies from 0 to 4.
a) Joins quantity: 0

| min | 0.010763 s |
| max | 0.405039 s |
| average | 0.071537 s |
| total | 7.15 s |
This test has no joins, so the results is the same from the previous test when fetching by an attribute.
b) Joins quantity: 1

| min | 0.012153 s |
| max | 0.632435 s |
| average | 0.299673 s |
| total | 29.98 s |
As might be expected, with 1 join the average time to a insert was much worst, about 4.27 times greater than with 0 joins.
c) Joins quantity: 2

| min | 0.021038 s |
| max | 0.633293 s |
| average | 0.29986 s |
| total | 29.96 s |
With 2 joins we needed almost the same time to process the select, as expected, once the fetching engine has already entered on the process’ join step, what is not needed with 0 joins.
d) Joins quantity: 3

| min | 0.025723 s |
| max | 0.621014 s |
| average | 0.303619 s |
| total | 30.36 s |
With 3 joins the average time was slightly worst again, as we might expect.
e) Joins quantity: 4

| min | 0.027883 s |
| max | 0.64675 s |
| average | 0.316077 s |
| total | 31.61 s |
Again, with 4 joins the average time was slightly worst, as we might expect.
Conclusion
The following table results from the tests:
| Test | Average Time | Total Time |
| 0 joins | 0.071537 s | 7.15 s |
| 1 join | 0.299673 s | 29.98 s |
| 2 joins | 0.29986 s | 29.96 s |
| 3 joins | 0.303619 s | 30.36 s |
| 4 joins | 0.316077 s | 31.61 s |
As we can see, we have a great variance from 0 to 1 join, but a small variance as the number of joins increases, due the way the select engine works.
This post, and the previous one, showed performance tests for Core Data that bring us information to analyse when use it in a project and what impact we would have when using it.
The next posts will present our analysis over the Magical Panda Active Record framework. We expect you are anxious as we are. Stay tuned!
This post is also available in Portuguese: Testes de performance do Core Data sobre SQLite – Parte 3.
Cocos2d for iPhone 0.99.4 – OpenGL ES Transparent Layer
Overview
According to Cocos2d website, Cocos2d for iPhone is a framework for building 2D games, demos, and other graphical/interactive applications. It is based on the cocos2d design: it uses the same concepts, but instead of using Python it uses Objective-C.
Their website says that Cocos2d for iPhone is:
- Easy to use: it uses a familiar API, and comes with lots of examples
- Fast: it uses the OpenGL ES best practices and optimized data structures
- Flexible: it is easy to extend, easy to integrate with 3rd party libraries
- Free: is open source, compatible both with closed and open source games
- Community supported: cocos2d has an active, big and friendly community (forum, IRC)
- AppStore approved: More than 550 AppStore games already use it, including many best seller games.
Cocos2d comes with an API that makes it simple to create an OpenGL ES based project, even if you are not an expert with OpenGL programming, once it has a nice encapsulation of some functionalities that are mostly used.
One negative point of using this kind of tool is that sometimes you get lost when it automates something that you didn’t want to be done for you. When trying to create a transparent OpenGL ES Layer with Cocos2d we saw that lot of people had this problem so we resolved to post about it.
Creating a Transparent OpenGL ES Layer with Cocos2d
Creating a transparent OpenGL ES Layer with Cocos2d v 0.99.4 was not an easy job, once its template code use a Macro to initialize a set of variables.
If you see the CC_DIRECTOR_INIT() macro, located on the ccMacros.h file, we have the following:
#define CC_DIRECTOR_INIT() \ do { \ \ window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; \ \ if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] ) \ [CCDirector setDirectorType:kCCDirectorTypeNSTimer]; \ \ CCDirector *__director = [CCDirector sharedDirector]; \ [__director setDeviceOrientation:kCCDeviceOrientationPortrait]; \ [__director setDisplayFPS:NO]; \ [__director setAnimationInterval:1.0/60]; \ \ EAGLView *__glView = [EAGLView viewWithFrame:[window bounds] \ pixelFormat:kEAGLColorFormatRGB565 \ depthFormat:0 \ preserveBackbuffer:NO]; \ \ [__director setOpenGLView:__glView]; \ \ [window addSubview:__glView]; \ [window makeKeyAndVisible]; \ \ } while(0); \
As we can see, this macro creates an EAGLView that has pixelFormat of type kEAGLColorFormatRGB565, which is 16 bits. In order to have transparency enabled in our EAGLView we need to create it using kEAGLColorFormatRGBA8 format, which is 32 bits.
We have lot of ways to solve it, such as changing that macro or initializing everything by ourselves. The important thing is to make sure to change the line:
EAGLView *__glView = [EAGLView viewWithFrame:[window bounds] \ pixelFormat:kEAGLColorFormatRGB565 \ depthFormat:0 \ preserveBackbuffer:NO]; \
to
EAGLView *__glView = [EAGLView viewWithFrame:[window bounds] \ pixelFormat:kEAGLColorFormatRGBA8 \ depthFormat:0 \ preserveBackbuffer:NO]; \
So, we will have a transparent layer when using:
glClearColor(0, 0, 0, 0);
In the above line, the last parameter indicates the opacity.
(http://www.khronos.org/opengles/sdk/1.1/docs/man/).
So that’s it! We hope you enjoy Cocos2d for iPhone and this tip helps you!
This post is also available in Portuguese: Cocos2d for iPhone 0.99.4 – Camada Transparente com OpenGL ES
Core Data over SQLite Performance Tests – Part 2
Following the last post, Core Data over SQLite Performance Tests – Part 1, when we introduced Core Data and defined the environment in which tests will run on, now we are going to start presenting you the results of this analysis.
As we defined before, this test will show the performance of 4 situations (see details on the previous post):
- Insert without join tables;
- Inserts with tables;
- Select without join tables;
- Select with join tables.
This post will cover the first 2 situations (inserts).
1. Inserts without join tables
This test tries to execute 10000 inserts on 5 different ways:
- Batch size: 1; Times: 10000;
- Batch size: 10; Times: 1000;
- Batch size: 100; Times: 100;
- Batch size: 1000; Times: 10;
- Batch size: 10000; Times: 1.
The results were the following:
a) Batch size: 1; Times: 10000

| min | 0.037017 s |
| max | 0.571604 s |
| average | 0.047213 s |
| total | 417.3 s |
As we can see on the chart, we have a slight variance between the inserts. Although the maximum chart value was 0.57 seconds, the average (0.047s) was much more close to the minimum value (0.037s). Also, we can see that the time needed to a insert does not changes significantly while the table increases.
b) Batch size: 10; Times: 1000

| min | 0.051545 s |
| max | 1.592167 s |
| average | 0.077328 s |
| total | 77.3 s |
This case shown us that, increasing the batch size to 10 we need, on average, about 1.64x more time to execute this batch. So, we can imagine that is much better to execute a big batch than lot of small batches. The test shown that to insert 10000 registries with batch size of 10 we needed 77.3 s, while with batch size of 1, to insert 10000 registries we needed 417.3 s.
c) Batch size: 100; Times: 100

| min | 0.221817 s |
| max | 0.733436 s |
| average | 0.276504 s |
| total | 27.7 s |
This case shown that increasing again the batch size, now to 100, we needed about 3.6x more time per batch than we needed with batch size of 10. So, we can deduce that the time per batch does not increases linearly as the batch size increases. But again, the total time to execute 10000 inserts was lower (27 s against 77 s).
d) Batch size: 1000; Times: 10

| min | 2.341496 s |
| max | 2.895445 s |
| average | 2.522845 s |
| total | 25.2 s |
Again, the same happened. As we increases the batch size to 1000 we needed 9.1x more time than we needed with batch size of 100. The total time was better than the previous, but is almost the same (25 s against 27 s).
e) Batch size: 10000; Times: 1 (10 repetitions with empty database)

| min | 19.171194 s |
| max | 24.020913 s |
| average | 22.2 s |
This time, increasing the batch size to 10000 we needed 8x more time than we needed with batch size of 1000, while we could imagine we would need more than 9.1. So, we needed only 22 s to execute 10000 inserts.
Conclusion
This tests resulted on the following table:
| Test | Average Time per Batch | Total Time |
| a | t1 or 0.047213 s | t2 or 417.3 s |
| b | 1.83 x t1 or 0.077328 s | t2/5.40 or 77.3 s |
| c | 5.86 x t1 or 0.276504 s | t2/0.066 or 27.7 s |
| d | 53.68 x t1 or 2.523 s | t2/0.060 or 25.2 s |
| e | 470.34 x t1 or 22.2 s | t2/0.053 or 22.2 s |
As we can see, for simple inserts (without joins) as we increases the batch size, the time needed to to execute that batch is greater, but, the total time is lower. So, with Core Data, when possible we should save data to database using batches.
2. Inserts with join tables
This test shows how the time to execute a insert increases as the number of join tables (in each insert) and number of rows increases. The number of joins varies from 0 to 3.
a) Joins quantity: 0

| min | 0.053622 s |
| max | 0.626013 s |
| average | 0.068631 s |
| total | 137.3 s |
This test has no joins, so the results is the same from the previous test.
b) Joins quantity: 1

| min | 0.617910 s |
| max | 0.353416 s |
| average | 0.080156 s |
| total | 160.3 s |
As might be expected, with 1 join the average time to a insert was 1.17 times greater than with 0 joins.
c) Joins quantity: 2

| min | 0.078214 s |
| max | 0.559592 s |
| average | 0.109143 s |
| total | 218.3 s |
With 2 joins we needed even more time to process an insert: 1.37 times more than with 1 join. Also, we can see that it seems that, as the database increases, we need more time to do inserts that was join tables.
d) Joins quantity: 3

| min | 0.093524 s |
| max | 0.650233 s |
| average | 0.135843 s |
| total | 271.7 s |
With 3 joins the average time was worst again: 1.244 times more than with 2 joins.
Conclusion
The following table results from the tests:
| Test | Average Time | Total Time |
| a | t1 or 0.068631 s | t2 or 137.3 s |
| b | 1.17 x t1 or 0.080156 s | 1.17 x t2 or 160.3 s |
| c | 1.59 x t1 or 0.109143 s | 1.59 x t2 or 218.3 s |
| d | 1.99 x t1 or 0.135843 s | 1.99 x t2 or 271.7 s |
As we can see, as the number of joins increases in a insert the time required to process it also increases. This increasing number is not linear.
The next post will present our results and conclusions for selects on Core Data, stay tuned!
This post is also available in Portuguese: Testes de performance do Core Data sobre SQLite – Parte 2
Core Data over SQLite Performance Tests – Part 1
Following the last post, iPhone Persistent Store Overview, I.ndigo begins the performance experiments series, on iPhone persistent store alternatives, with Core Data, Apple’s official framework for this purpose.
Introduction
Core Data for iPhone was introduced in the 3.0 version of the SDK, although it was previously available for Mac OSX. The framework implements an Object Graph Manager, giving applications the ability to manage data, including inserting new records, applying changes, undoing and redoing them and also the ability persist them.
It provides a high level API that abstracts all data management rules, as unique identifiers, model consistency and data validation, insertion, update and deletion, as well as data store creation and operation.
Data modeling abstraction is achieved by Xcode’s integrated graphical data modeling tool, where the developer must describe an entity-relationship diagram representing and the system’s data. Xcode then generates all the classes and files needed to manage and optionally persist the data.
The persistent store layer abstracts the database and file store to the developer. The iPhone SDK provides SQLite and a binary format and the Mac OSX SDK also provides XML persistence. Either way, the implementation details stay apart from the developer, who should only care about objects and its properties.
Core Data Architecture
To implement the previously described functionality, a flexible and solid architecture was created, as seen in the following diagram:
- NSManagedObjectModel: Created on run-time, based on the project’s data model, which is designed by the developer in the integrated graphical tool, represents the hole system’s data;
- NSManagedObject: Represents each entity and its properties, modeled by the developer. Those properties includes attributes and relationships;
- NSManagedObjectContext: Provides all the mentioned functionality to the Managed Objects, as fetching and deletion; It is aware of and has access to the Persistent Store Coordinator;
- NSPersistentStoreCoordinator: Provides an interface to the data persistence layer.
As mentioned before, Core Data is not limited to data persistence. All aspects of data management are separated from the persistence layer and can be used without it.
Testing Methodology
Environment
| iPhone 3G 8GB | Xcode 3.2.3 | iPhone SDK 4.0 | iOS 4.0 | Release Configuration |
Data Model
Testing Scenario
SQLite was chosen over binary format because it is the only type that is capable of partial object graph loading, making use of the lazy fetching feature, what is very important for the powerful, though limited iPhone hardware capabilities.
Also, a 10000 lines database, or 10000 persistent objects, was considered enough for performance testing purpose. Core Data persists its objects by saving all the NSManagedObjects present in the NSManagedObjectContext by once. Therefore, the testing methodology for the insertion tests included batch operations, with different of objects quantity per save, as the following table describes:
| Batch size | Times |
| 1 | 10000 |
| 10 | 1000 |
| 100 | 100 |
| 1000 | 10 |
| 10000 | 1 |
The following testes will be performed:
- Insert without join tables
The main objective of this test is to determine the insertion performance degradation, according to the database size. Therefore, following the previous ta ble, 10000 objects will be persisted. - Insert with join tables
This test purpose is to decide how much the number of table joins affect the performance. Therefore, a smaller database will be used (2000 lines) with join quantity from none to four. - Select without join tables
In this test two approaches will be considered: fetching objects by its attributes and getting them by their identifier. The performance is going to be measured, until the persisted object’s count reaches 10000. - Select with join tables
This tests aims to identify the selection performance degradation, according to the number of object relationships (table joins). Since this is the heaviest test, once the data must be inserted and then selected accordingly, data will be selected after each 100 objects are inserted (atomically).
All tests have as main objective to determine the performance degradation of Core Data in relation to the amount of persisted data and the number of relationships between the data entities.
The next post will present our results and conclusions, stay tuned!
This post is also available in Portuguese: Testes de performance do Core Data sobre SQLite – Parte 1
iPhone Persistent Store Overview
Storing information over iPhone apps is a task that needs to be carefully taken and analyzed. We know an iPhone has limited resources that need to be used and released, properly. The problem is: what happens when you have an app that stores and load large amount of data (e.g, a Sales Force Automation Applications)?
We don’t know how the application will respond to these data access with the time (as the database size increases). Moreover, we have lot of third-party options of persistence layer, or layers responsible to manage data access and data mapping to objects.
These third-party options may have other problems that could resulting in performance loss with the time. So, choosing the best for each situation isn’t an easy job.
Thus, I.ndigo has decided to begin a series of performance experiments on the most known options of Persistent Store, and obviously, the Core Data purely implemented.
Searching on the web for options resulted on the following list of technologies:
| Core Data | FMDB | Magical Panda | Mogenerator | OmniDataObjects | iphone-rsdb | SQLite | SQLitePersistentObjects | |
| Abstraction level | object graph manager | SQLite wrapper | ActiveRecord (over Core Data) | object graph manager | Core Data API implementation (over SQLite) | SQLite wrapper (based on fmdb) | - | ActiveRecord over SQLite |
| belongs_to implementation | yes | yes | yes | yes | yes | yes | yes | yes |
| has_many implementation | yes | yes | yes | yes | yes | yes | yes | yes |
| many_to_many implementation | yes | yes | yes | yes | N/A | no | yes | no |
| SQL | no | yes | no | no | no | yes | yes | yes |
| Lazy Loading | yes | no | yes | yes | yes | no | yes | no |
| License | iPhone Program | MIT | MIT | N/A | MIT | Apache 2.0 | Public Domain | New BSD License |
Next posts will cover a serie of tests executed on some of these technologies and comparisons between them in several scenarios.
Stay tuned!
This post is also available in Portuguese: Visão Geral de iPhone Persistent Store


