ios - Performance with a lot of string operations comparing objective C and swift -
i have lot of string operations concatenating, replacing , finding indexes of large texts (10000 characters) ...
the operations slow. same doing in java/android faster.
i asking if same in objective c faster.
i ios newby , know swift far (so cant try simply), thats why asking if objective c swift-bridging faster ?
update have lot of substring operations in loop (also replace), concatenates new string. newtext & stext of type string, stext has 10000 characters, loop have 100 iterations:
newtext=newtext + stext.substring(istart,endindex: iend); func substring(startindex: int, endindex: int) -> string { //println("substring" + self); var start = advance(self.startindex, startindex) var end = advance(self.startindex, endindex) return self.substringwithrange(range<string.index>(start: start, end: end)) }
update 2 performance test (replace string) string, nsstring, cfstring
from information provided (thanks lot), seems not difference between objective-c , swift. more stringtype use. made performance test types: string, nsstring , cfstring
func performance_test_with_strings(){ var stextnsstring:nsstring="<1000 character> searchstring end"; var stextstring=string(stextnsstring); //var stextcfstring:cfmustring=stextnsstring cfstring; var storeplace="searchstring"; var stextcfstring:cfmutablestringref=cfstringcreatemutable(nil, 0); cfstringappend(stextcfstring, stextstring as! cfmutablestringref); var storeplacecfstring="searchstring" cfstring; var storeplacecfstring2="mynewstring" cfstring; var chrono1:chronometer=chronometer(); chrono1.start(); var i=0;i<10000;i++ { var newtext=stextnsstring.stringbyreplacingoccurrencesofstring(storeplace, withstring: "mynewstring"); } chrono1.zwischenstand("after replacing nsstring"); var i=0;i<10000;i++ { var newtext=stextstring.stringbyreplacingoccurrencesofstring(storeplace, withstring: "mynewstring"); } chrono1.zwischenstand("after replacing string"); //cfshow(cfmutablestring); var i=0;i<5000;i++ { // compare correct i'll have 2 replacments in loop of 5000 iterations specialreplace(&stextcfstring,swhat: "searchstring",sto: "mynewstring"); specialreplace(&stextcfstring,swhat: "mynewstring",sto: "searchstring"); } chrono1.zwischenstand("after replacing cfstring"); chrono1.showmeasures(); exit(0); } func specialreplace(inout stext:cfmutablestringref,swhat:string, sto:string){ var cfrange = cfstringfind(stext, swhat cfstring, nil); cfstringreplace(stext, cfrange, sto cfstring); } class chronometer: nsobject { var mearures:[(string,double)]=[(string,double)](); var starttime = nsdate(); // <<<<<<<<<< end time var lasttime:double=0; func start(){ starttime = nsdate(); // <<<<<<<<<< end time } func zwischenstand(mytext:string){ var zwischenzeit = nsdate(); let timeinterval: double = zwischenzeit.timeintervalsincedate(starttime); let actualtimeconsumed=timeinterval-lasttime; mearures.append((mytext,actualtimeconsumed)); var textshow=mytext + " actual : " + string(stringinterpolationsegment: actualtimeconsumed); textshow=textshow + " total :" + string(stringinterpolationsegment: timeinterval); println(textshow); lasttime=timeinterval; } func showmeasures(){ var total:double=0; var i=0 ; < mearures.count ; i++ { let text=mearures[i].0; let measure=mearures[i].1; println(text + " : " + string(stringinterpolationsegment: measure)); total = total + measure; } println("total : " + string(stringinterpolationsegment: total)); } }
after replacing nsstring actual : 1.15460801124573 total :1.15460801124573
after replacing string actual : 1.15148597955704 total :2.30609399080276
after replacing cfstring actual : 0.323610007762909 total :2.62970399856567
after replacing nsstring : 1.15460801124573
after replacing string : 1.15148597955704
after replacing cfstring : 0.323610007762909
total : 2.62970399856567
so conclusion case best use cfstring.
is test correct ?
one thing note swift attempts unicode handling properly. java , cocoa's nsstring
not this. both assume string encoded in utf-16 , each character takes 1 16 bit integer. therefore string handling works called basic multilingual plane
with nsstring
, in java, finding nth "character" easy, index nth item in array, second part of surrogate pair. can't tell if nth character except scanning previous characters make sure none of them 2 word characters.
swift properly, @ expense of lot of linear (aka slow) scanning through strings.
Comments
Post a Comment