解析Objective-C幾種字串處理速度資料測試

來源:互聯網
上載者:User

Objective-C幾種字串處理速度資料測試是本文要介紹的內容,本文內容不多,主要是基於代碼實現資料測試,具體內容,先來看本文詳細代碼。

測試機器 2.4 GHz Intel Core 2Duo 2GB 667 MHz DDR2   GCC 4.2

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

小結:解析Objective-C幾種字串處理速度資料測試的內容介紹完了,希望通過本文的學習能讀你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.