Improve performance of hash (for use as dictionary keys)

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@21691 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2005-08-31 09:34:33 +00:00
parent 643c232713
commit d7dcbb06b7
4 changed files with 66 additions and 3 deletions

View file

@ -1,8 +1,14 @@
2005-08-31 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSNumber.m: Improve poor hash function.
* Source/NSConcreteNumber.m: Improve poor hash function.
* Testing/benchmark.m: Add some NSNumber tests.
2005-08-30 Saso Kiselkov <diablos@manga.sk>
* Headers/Foundation/NSSortDescriptor.h, Source/NSSortDescriptor.m:
New files
* Headesr/Foundation/Foundation.h, Source/GNUmakefile: Add them to
* Headers/Foundation/Foundation.h, Source/GNUmakefile: Add them to
list.
2005-08-25 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -121,7 +121,7 @@
val.d = [self doubleValue];
for (i = 0; i < sizeof(double); i++)
{
hash += val.c[i];
hash = (hash << 5) + hash + val.c[i];
}
return hash;
}

View file

@ -2382,7 +2382,7 @@ static Class doubleNumberClass;
val.d = [self doubleValue];
for (i = 0; i < sizeof(double); i++)
{
hash += val.c[i];
hash = (hash << 5) + hash + val.c[i];
}
return hash;
}

View file

@ -366,6 +366,62 @@ bench_dict()
AUTO_END;
}
void
bench_number()
{
int i;
int j;
NSMutableDictionary *dict;
NSNumber *n[MAX_COUNT*10];
AUTO_START;
printf("NSNumber\n");
START_TIMER;
for (i = 0; i < MAX_COUNT*10; i++)
{
n[i] = [NSNumber numberWithInt: i];
}
END_TIMER;
PRINT_TIMER("NSNumber (creation) \t\t");
START_TIMER;
for (i = 0; i < MAX_COUNT; i++)
{
[n[i] hash];
}
END_TIMER;
PRINT_TIMER("NSNumber (hash) \t\t");
dict = [NSMutableDictionary dictionaryWithCapacity: MAX_COUNT];
START_TIMER;
for (i = 0; i < MAX_COUNT*10; i++)
{
[dict setObject: n[i] forKey: n[i]];
}
END_TIMER;
PRINT_TIMER("NSNumber (dictionary setObject:) \t\t");
START_TIMER;
for (i = 1; i < MAX_COUNT; i++)
{
[n[i] isEqual: n[i-1]];
}
END_TIMER;
PRINT_TIMER("NSNumber (isEqual:)\t\t");
START_TIMER;
for (i = 0; i < MAX_COUNT; i++)
{
[n[i] copyWithZone: NSDefaultMallocZone()];
}
END_TIMER;
PRINT_TIMER("NSNumber (copy)\t\t");
AUTO_END;
}
void
bench_str()
{
@ -632,6 +688,7 @@ int main(int argc, char *argv[], char **env)
pool = [NSAutoreleasePool new];
printf(" Test \t\t\t\t time (sec) \t index\n");
bench_object();
bench_number();
bench_str();
bench_array();
bench_dict();