• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

转载自: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];
  • }

  • 鲜花

    握手

    雷人

    路过

    鸡蛋
    该文章已有0人参与评论

    请发表评论

    全部评论

    专题导读
    上一篇:
    iOS生成QRCode(QR-Code-Encoder-for-Objective-C)发布时间:2022-07-12
    下一篇:
    Objective-C再谈OC指针,对比C++/Java/Swift发布时间:2022-07-12
    热门推荐
    热门话题
    阅读排行榜

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap