Code: Select all
+ (NSString *) uniqueString
{
CFUUIDRef uuid = CFUUIDCreate(NULL);
NSString *uString = (NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
return [uString autorelease];
}
Code: Select all
-(unsigned int)hash {
return [[self valueForProperty:kGCMKUIDProperty] hash];
}
Code: Select all
- (unsigned int) hash
{
return( ((unsigned int) self >> 4) |
(unsigned int) self << (32 - 4));
}
Code: Select all
#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
NSString *newUniqueString()
{
CFUUIDRef uuid;
NSString *s;
uuid = CFUUIDCreate(NULL);
s = (NSString *) CFUUIDCreateString(NULL, uuid);
CFRelease( uuid);
return( s);
}
static unsigned int linear_test( unsigned int index)
{
return( index);
}
static unsigned int random_test( unsigned int index)
{
return( rand());
}
static unsigned int uuid_test( unsigned int index)
{
unsigned int hash;
NSString *s;
s = newUniqueString();
hash = [s hash];
[s release];
return( hash);
}
#define RUNS 1000000
#define MASK 0xFF
void test( unsigned int (*hashGenerator)( unsigned int index))
{
unsigned int spread[ MASK + 1];
unsigned int i;
unsigned long total;
unsigned int mean;
NSString *s;
unsigned int hash;
long diff;
memset( spread, 0, sizeof( spread));
for( i = 0; i < RUNS; i++)
{
hash = (*hashGenerator)( i);
spread[ hash & MASK]++;
}
mean = (RUNS + MASK) / (MASK + 1);
total = 0;
for( i = 0; i <= MASK; i++)
{
diff = (long) spread[ i] - (long) mean;
printf( "% 3d : %d (%d)\n", i, spread[ i], diff);
total += diff < 0 ? -diff : diff;
}
printf( "Deviation %d total, %d average\n\n", total, (total + MASK) / (MASK + 1));
}
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
test( linear_test);
srand( 0x1848);
test( random_test);
test( uuid_test);
[pool release];
return 0;
}
First you have to think about your objects. What does isEqual: mean with respect to them ? As you are putting a UUID - of which not two can be the same - into each of them does that mean, that no two objects will ever be equal ? Or can two objects with the same UUID exist (possibly because they where fetched twice from some storage ?
The UUID hash you used is not too bad, it only gets around a 25% spread (by visual inspection of the output...) , but that shouldn't give pathetic performance. It'd be interesting to know the real life consequences, if you find a better hash
altimac wrote:ok, i then conclude that the current hash method i use is good and not responsible for the "slow" NSDictionary insertion of my objects...
thanks to have confirmed that, i would not have been able to conclude that myself without your tool !
altimac wrote:But i noticed, using shark, that inserting my objects in a NSDictionary (using the UUID of the object as a key) is "slow"
Return to “Article Discussion”
Users browsing this forum: No registered users and 0 guests