如果我写的话会有性能提升吗
- (NSString *)helloStringWithName:(NSString *)name
static NSString *formatString = @"Hello %@!";
return [NSString stringWithFormat:formatString, name];
}
代替
- (NSString *)helloStringWithName:(NSString *)name
return [NSString stringWithFormat:@"Hello %@!", name];
}
??
如果我猜的话,我会认为每次代码运行时都会创建和自动释放后者,但我猜编译器足够聪明,知道这里要做什么。
最佳答案
如果您尝试此操作(菜单 -> 产品 -> 生成输出 -> 程序集文件)- 您会注意到 clang 下的输出几乎相同 - 由于剩余的额外变量分配和调试信息,只有很小的变化。
所以简而言之 - 没有真正的区别;尽管我猜静态的可能更容易调试。
.align 4, 0x90
"-[Foo helloStringWithName1:]": ## @"\01-[Foo helloStringWithName1:]"
.cfi_startproc
Lfunc_begin0:
.loc 1 15 0 ## /Users/dirkx/tmp/ccccc/ccccc/main.m:15:0
## BB#0:
pushq %rbp
Ltmp2:
.cfi_def_cfa_offset 16
Ltmp3:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp4:
.cfi_def_cfa_register %rbp
subq $48, %rsp
movq %rdi, -8(%rbp)
movq %rsi, -16(%rbp)
movq %rdx, %rdi
callq _objc_retain
movq %rax, -24(%rbp)
.loc 1 17 5 prologue_end ## /Users/dirkx/tmp/ccccc/ccccc/main.m:17:5
Ltmp5:
movq L_OBJC_CLASSLIST_REFERENCES_$_(%rip), %rax
movq "-[Foo helloStringWithName1:].formatString"(%rip), %rdx
movq -24(%rbp), %rcx
movq L_OBJC_SELECTOR_REFERENCES_(%rip), %rsi
movq %rax, %rdi
movb $0, %al
callq _objc_msgSend
movq %rax, %rdi
callq _objc_retainAutoreleasedReturnValue
movabsq $0, %rsi
leaq -24(%rbp), %rcx
movl $1, -28(%rbp)
.loc 1 18 1 ## /Users/dirkx/tmp/ccccc/ccccc/main.m:18:1
movq %rcx, %rdi
movq %rax, -40(%rbp) ## 8-byte Spill
callq _objc_storeStrong
Ltmp6:
.loc 1 18 1 ## /Users/dirkx/tmp/ccccc/ccccc/main.m:18:1
movq -40(%rbp), %rax ## 8-byte Reload
movq %rax, %rdi
callq _objc_autoreleaseReturnValue
.loc 1 17 5 ## /Users/dirkx/tmp/ccccc/ccccc/main.m:17:5
Ltmp7:
addq $48, %rsp
popq %rbp
ret
Ltmp8:
Lfunc_end0:
.cfi_endproc
对比
.align 4, 0x90
"-[Foo helloStringWithName2:]": ## @"\01-[Foo helloStringWithName2:]"
.cfi_startproc
Lfunc_begin1:
.loc 1 20 0 ## /Users/dirkx/tmp/ccccc/ccccc/main.m:20:0
## BB#0:
pushq %rbp
Ltmp11:
.cfi_def_cfa_offset 16
Ltmp12:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp13:
.cfi_def_cfa_register %rbp
subq $48, %rsp
movq %rdi, -8(%rbp)
movq %rsi, -16(%rbp)
movq %rdx, %rdi
callq _objc_retain
leaq L__unnamed_cfstring_2(%rip), %rdx <--------------------
movq %rax, -24(%rbp)
.loc 1 21 5 prologue_end ## /Users/dirkx/tmp/ccccc/ccccc/main.m:21:5
Ltmp14:
movq L_OBJC_CLASSLIST_REFERENCES_$_(%rip), %rax
movq -24(%rbp), %rcx
movq L_OBJC_SELECTOR_REFERENCES_(%rip), %rsi
movq %rax, %rdi
movb $0, %al
callq _objc_msgSend
movq %rax, %rdi
callq _objc_retainAutoreleasedReturnValue
movabsq $0, %rsi
leaq -24(%rbp), %rcx
movl $1, -28(%rbp)
.loc 1 22 1 ## /Users/dirkx/tmp/ccccc/ccccc/main.m:22:1
movq %rcx, %rdi
movq %rax, -40(%rbp) ## 8-byte Spill
callq _objc_storeStrong
Ltmp15:
.loc 1 22 1 ## /Users/dirkx/tmp/ccccc/ccccc/main.m:22:1
movq -40(%rbp), %rax ## 8-byte Reload
movq %rax, %rdi
callq _objc_autoreleaseReturnValue
.loc 1 21 5 ## /Users/dirkx/tmp/ccccc/ccccc/main.m:21:5
Ltmp16:
addq $48, %rsp
popq %rbp
ret
Ltmp17:
Lfunc_end1:
.cfi_endproc
作为关键引用:
Lfunc_end2:
.cfi_endproc
.section __DATA,__data
.align 3 ## @"\01-[Foo helloStringWithName1:].formatString"
"-[Foo helloStringWithName1:].formatString":
.quad L__unnamed_cfstring_
.section __TEXT,__cstring,cstring_literals
l_.str: ## @.str
.asciz "Hello 1 %@!"
和
.section __TEXT,__cstring,cstring_literals
l_.str1: ## @.str1
.asciz "Hello 2 %@!"
关于iphone - NSString——静态的还是内联的?有任何性能提升吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18250064/
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
我在思考流量控制的最佳实践。我应该走哪条路?1)不要检查任何东西并让程序失败(更清晰的代码,自然的错误消息):defself.fetch(feed_id)feed=Feed.find(feed_id)feed.fetchend2)通过返回nil静默失败(但是,“CleanCode”说,你永远不应该返回null):defself.fetch(feed_id)returnunlessfeed_idfeed=Feed.find(feed_id)returnunlessfeedfeed.fetchend3)抛出异常(因为不按id查找feed是异常的):defself.fetch(feed_id
我正在使用Ruby解决一些ProjectEuler问题,特别是这里我要讨论的问题25(Fibonacci数列中包含1000位数字的第一项的索引是多少?)。起初,我使用的是Ruby2.2.3,我将问题编码为:number=3a=1b=2whileb.to_s.length但后来我发现2.4.2版本有一个名为digits的方法,这正是我需要的。我转换为代码:whileb.digits.length当我比较这两种方法时,digits慢得多。时间./025/problem025.rb0.13s用户0.02s系统80%cpu0.190总计./025/problem025.rb2.19s用户0.0
eruby和erb有什么区别?哪些考虑因素会促使我选择其中之一?我的应用程序正在为网络设备(路由器、负载平衡器、防火墙等)生成配置文件。我的计划是对配置文件进行模板化,在源文件中使用嵌入式ruby(通过eruby或erb)来执行诸如迭代生成路由器的所有接口(interface)配置block之类的操作(这些block都非常相似,仅在标签上有所不同和IP地址)。例如,我可能有这样一个配置模板文件:hostnamesample-routerlogging10.5.16.26当通过嵌入式ruby解释器(erb或eruby)运行时,会产生以下输出:hostnamesample-rout
我正在寻找一个用ruby演示计时器的在线示例,并发现了下面的代码。它按预期工作,但这个简单的程序使用30Mo内存(如Windows任务管理器中所示)和太多CPU有意义吗?非常感谢deftime_blockstart_time=Time.nowThread.new{yield}Time.now-start_timeenddefrepeat_every(seconds)whiletruedotime_spent=time_block{yield}#Tohandle-vesleepinteravalsleep(seconds-time_spent)iftime_spent
如果用户是所有者,我有一个条件来检查说删除和文章。delete_articleifuser.owner?另一种方式是user.owner?&&delete_article选择它有什么好处还是它只是一种写作风格 最佳答案 性能不太可能成为该声明的问题。第一个要好得多-它更容易阅读。您future的自己和其他将开始编写代码的人会为此感谢您。 关于ruby-on-rails-如果条件与&&,是否有任何性能提升,我们在StackOverflow上找到一个类似的问题:
如果我想要“00001”而不是“1”,除了我自己写填零方法之外,有没有内置的方法可以帮助我为整数填零? 最佳答案 puts"%05d"%1#00001参见:String::%,Kernel::sprintf这是正在发生的事情。%左侧的"%05d"是C风格的格式说明符。%右边的变量就是要格式化的东西。格式说明符可以像这样解码:%-格式说明符的开头0-用前导零填充5-长度为5个字符d-被格式化的是一个整数如果你要格式化多个东西,你会把它们放在一个数组中:"%d-%s"%[1,"One"]#=>1-one
我编写了一个Ruby应用程序,它可以解析来自不同格式html、xml和csv文件的源中的大量数据。我如何找出代码的哪些区域花费的时间最长?有没有关于如何提高Ruby应用程序性能的好资源?或者您是否有任何始终遵循的性能编码标准?例如,你总是用加入你的字符串吗?output=String.newoutput或者你会使用output="#{part_one}#{part_two}\n" 最佳答案 好吧,有一些众所周知的做法,例如字符串连接比“#{value}”慢得多,但是为了找出您的脚本在哪里消耗了大部分时间或比所需时间更多,您需要进行分