I'm coming from a C++/STL world and I wanted to check how objective-c containers are in comparison to stl.
I wanted to compare an array of numbers but the only way to add a number to an NSArray is using NSNumber which is utterly slow and drank my ram empty so I guess I need to dealloc them manually. But I don't want to test side effects so I just added [NSNull null] into the array.
The results of adding 10k things into array 1k times:
NSArray - 0.923411 seconds
vector<int> - 0.129984 seconds
I thought it might be allocations and deallocations so I set the number of arrays(imax in the code) to 1 and number of additions to 10000000(jmax) but it was even slower
NSArray - 2.19859 seconds
vector<int> - 0.223471 seconds
Edit:
As mentioned in the comments the constant increasing size of the array might be the problem so I made NSArray using arrayWithCapacity, but vector with reserve too and it was even slower than before(!) (imax = 1, jmax = 10000000).
NSArray - 2.55942
vector<int> - 0.19139
End edit
Why is this so slow?
My code for reference:
#import <Foundation/Foundation.h> #include <vector> #include <iostream> #include <time.h> using namespace std; int main (int argc, const char * argv[]) { int imax = 1000; int jmax = 10000; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; cout << "Vector insertions" << endl; clock_t start = clock(); for(int i = 0; i < imax; i++) { vector<int> *v = new vector<int>(); for(int j = 0; j < jmax; j++) { v->push_back(j); } delete v; } double interval = (clock() - start) / (double)CLOCKS_PER_SEC; cout << interval << " seconds" << endl; cout << "NSArray insertions" << endl; start = clock(); for(int i = 0; i < imax; i++) { NSMutableArray *v = [[NSMutableArray alloc] init]; for(int j = 0; j < jmax; j++) { [v addObject:[NSNull null]]; } [v dealloc]; } interval = (clock() - start) / (double)CLOCKS_PER_SEC; cout << interval << " seconds" << endl; [pool drain]; return 0; }