转载自:http://www.cocoachina.com/bbs/read.php?tid-17652-fpage-9.html
仅供参考
测试机器 2.4 GHz Intel Core 2Duo 2GB 667 MHz DDR2 GCC 4.2
- - (void)testStringSpeed:(id)sender
- {
- NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
- [textField setStringValue:@""];
- int testi,testnum=10;
- float c,tm=0.0;
- for(testi=0;testi<testnum;testi++){
- NSDate *beg=[NSDate date];
- int i,n=10000000;
- for(i=0;i<n;i++){
- //avg=0.030204
- /*
- {
- //avg=0.594266 内存基本稳定不变
- NSString *t=[[NSString alloc] initWithString:@"abccc"];
- [t release];
- }*/
-
- /*
- {
- //avg=0.026101 内存基本稳定不变
- NSString *astring = @"abcc";
- }*/
-
- /*
- {
- //avg=0.278873 内存基本稳定不变
- NSString *astring = [[NSString alloc] init];
- astring = @"abcc";
- [astring release];
- }*/
-
- /*
- {
- //avg=2.737541 内存基本稳定不变
- char *Cstring = "abcc";
- NSString *astring = [[NSString alloc] initWithCString:Cstring];
- [astring release];
- }*/
-
- /*
- {
- //avg=3.619728 内存增长过快
- NSString *a=[NSString stringWithString:@"abcc"];
- }*/
-
- /*
- {
- //太长时间,内存增长过快
- NSString *a=[NSString stringWithFormat:@"abcc%d",i];
- }
- */
-
- /*
- {
- //avg=0.034632 内存基本稳定不变
- char a[]="abcc";
- }*/
-
-
- /*
- {
- //18.1555 内存稍有增长
- NSString *a=[[NSString alloc] initWithFormat:@"abcc%d",i];
- [a release];
- }*/
-
- /*
- {
- //avg=2.276076 内存基本稳定不变
- char a[32];
- sprintf(a,"abcc%d",i);
- }*/
-
- /*
- {
- //太长时间,内存增长过快
- NSMutableString *a=[[NSMutableString alloc] init];
- [a stringByAppendingFormat:@"abcc%d",i];
- [a release];
- }*/
- }
- c=[[NSDate date] timeIntervalSinceDate:beg];
- tm+=c;
- [textField setStringValue:[NSString stringWithFormat:@"%@\n%d=%f",[textField stringValue],testi+1,c]];
- }
- [textField setStringValue:[NSString stringWithFormat:@"%@\navg=%f",[textField stringValue],(float)tm/testnum]];
- [pool release];
- }
|