Objective-C几种字符串处理速度的测试数据

转载自: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

 
  1. - (void)testStringSpeed:(id)sender
  2. {
  3.     NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
  4.     [textField setStringValue:@""];
  5.     int testi,testnum=10;
  6.     float c,tm=0.0;
  7.     for(testi=0;testi<testnum;testi++){
  8.         NSDate *beg=[NSDate date];
  9.         int i,n=10000000;
  10.         for(i=0;i<n;i++){
  11.             //avg=0.030204
  12.             /*
  13.             {
  14.                 //avg=0.594266 内存基本稳定不变
  15.                 NSString *t=[[NSString alloc] initWithString:@"abccc"];
  16.                 [t release];
  17.             }*/
  18.             
  19.             /*
  20.             {
  21.                 //avg=0.026101 内存基本稳定不变
  22.                 NSString *astring = @"abcc";
  23.             }*/
  24.             
  25.             /*
  26.             {
  27.                 //avg=0.278873 内存基本稳定不变
  28.                 NSString *astring = [[NSString alloc] init];
  29.                 astring = @"abcc";
  30.                 [astring release];
  31.             }*/
  32.             
  33.             /*
  34.             {
  35.                 //avg=2.737541 内存基本稳定不变
  36.                 char *Cstring = "abcc";
  37.                 NSString *astring = [[NSString alloc] initWithCString:Cstring];
  38.                 [astring release];
  39.             }*/
  40.             
  41.             /*
  42.             {
  43.                 //avg=3.619728 内存增长过快
  44.                 NSString *a=[NSString stringWithString:@"abcc"];
  45.             }*/
  46.             
  47.             /*
  48.             {
  49.                 //太长时间,内存增长过快
  50.                 NSString *a=[NSString stringWithFormat:@"abcc%d",i];
  51.             }
  52.              */
  53.             
  54.             /*
  55.             {
  56.                 //avg=0.034632   内存基本稳定不变
  57.                 char a[]="abcc";
  58.             }*/
  59.             
  60.             
  61.             /*
  62.             {
  63.                 //18.1555  内存稍有增长
  64.                 NSString *a=[[NSString alloc] initWithFormat:@"abcc%d",i];
  65.                 [a release];
  66.             }*/
  67.             
  68.             /*
  69.             {
  70.                 //avg=2.276076   内存基本稳定不变
  71.                 char a[32];
  72.                 sprintf(a,"abcc%d",i);
  73.             }*/
  74.             
  75.             /*
  76.             {
  77.                 //太长时间,内存增长过快
  78.                 NSMutableString *a=[[NSMutableString alloc] init];
  79.                 [a stringByAppendingFormat:@"abcc%d",i];
  80.                 [a release];
  81.             }*/
  82.         }
  83.         c=[[NSDate date] timeIntervalSinceDate:beg];
  84.         tm+=c;
  85.         [textField setStringValue:[NSString stringWithFormat:@"%@\n%d=%f",[textField stringValue],testi+1,c]];
  86.     }
  87.     [textField setStringValue:[NSString stringWithFormat:@"%@\navg=%f",[textField stringValue],(float)tm/testnum]];
  88.     [pool release];
  89. }
posted @ 2011-01-12 14:02  鬼手如冰  阅读(525)  评论(0编辑  收藏  举报