Re: speed of alloc/init for NSNumber
Re: speed of alloc/init for NSNumber
- Subject: Re: speed of alloc/init for NSNumber
- From: Nicko van Someren <email@hidden>
- Date: Sun, 18 Jan 2004 23:12:46 +0000
On 18 Jan 2004, at 22:22, Nat! wrote:
If you go above a very few thousand you may start worrying :)
Well to get a handle on how much time you are losing on allocation and
deallocation try code like this
...
Takes 2 minutes to type and gives hard facts.
Changing the code to run ten rounds of 1,000,000 allocations, on a 1GHz
17" PowerBook G4, I get:
Nicko-van-Somerens-Computer:~ nicko$ time ./alloctest
2004-01-18 22:51:10.181 alloctest[20005] Start
2004-01-18 22:51:26.905 alloctest[20005] Stop
real 0m16.853s
user 0m11.860s
sys 0m0.090s
Nicko-van-Somerens-Computer:~ nicko$
So the inner loop of:
[[[NSNumber alloc] initWithInt:1848] release];
Takes about 1.195 microseconds of CPU time, which represents nearly
1,200 CPU cycles!
Changing the code to put the items in a mutable array and releases them
all at the end of each rounds (new code below) slows it down a great
deal:
2004-01-18 23:03:15.192 alloctest[20129] Start
2004-01-18 23:04:08.312 alloctest[20129] Stop
real 0m53.253s
user 0m42.070s
sys 0m1.290s
So allocation, initialisation, adding to an array (which does a
retain), and a release, and then the share of the bulk release when we
remove all objects, takes about 4.34 microseconds or 4,300 cycles.
Note that changing the code to use initWithCapacity: on the array takes
about half a second off of the CPU time:
2004-01-18 23:05:12.408 alloctest[20152] Start
2004-01-18 23:06:06.861 alloctest[20152] Stop
real 0m54.593s
user 0m41.560s
sys 0m1.080s
Cheers,
Nicko
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
unsigned int i, j;
NSMutableArray *ma;
ma = [[NSMutableArray alloc] init];
NSLog( @"Start");
for( j = 0; j < 10; j++) {
for( i = 0; i < 1000000; i++) {
id x = [[NSNumber alloc] initWithInt:1848];
[ma addObject: x];
[x release];
}
[ma removeAllObjects];
}
NSLog(@"Stop");
[pool release];
return 0;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.