在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.先看字符串的object_id
str1="Anleb" str2="Anleb" puts str1.object_id puts str2.object_id 输出: s1=false s2=false s3=true s4=true s5=1 s6=1 puts s1.object_id puts s2.object_id puts s3.object_id puts s4.object_id puts s5.object_id puts s6.object_id 输出: n1=:"id" n2=:id p n1.object_id p n2.object_id 输出: str1="Anleb" puts str1 puts str1.object_id #22952460 str1=str1+"boy" puts str1 puts str1.object_id #22952430 说明字符串相加是产生新的对象 str1="Anleb" puts str1 puts str1.object_id #22952460 str1=str1 << "boy" puts str1 puts str1.object_id #22952460 说明利用 << 却不会New出新的对象,也减少了内存的开销 require 'benchmark' n1="abc" n2="abc" n3="abc" Benchmark.bm do |bm| bm.report("<<") do 10000.times {n1 << "abc"} end bm.report("+=") do 10000.times { n2+="abc"} end bm.report("concat") do 10000.times { n3.concat("abc")} end end 输出: user system total real << 0.000000 0.000000 0.000000 ( 0.000000) += 0.187000 0.063000 0.250000 ( 0.266000) concat 0.016000 0.000000 0.016000 ( 0.015000) str1="anleb" puts str1 puts str1.object_id str2=str1.capitalize puts str1 puts str2 puts str2.object_id str3=str1.capitalize! puts str1 puts str3 puts str3.object_id 输出: str1="Anleb" str2=str1 p str1.object_id p str1.object_id str3=str1.clone p str3.object_id str4=str1.dup p str4.object_id str5=String.new(str1) p str5.object_id 输出: |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论