ID:907512
 
Keywords: cpu, list, procs, usage
I'm unsure of wether or not this is the correct placement for this topic, though I suppose it could be moved at a later time.

I have recently been fiddling with text and list a lot, for a project I've slowly been developing alone.





As you can see, I ran two seperate test on these; seeing how much cpu was use uppon 100 thousand runs and 1 million runs simultaneously.

I'm quite pleased with these results, uppon my first test it resulted in almost 9 total cpu used for the Reverse() proc(per 100 thousand calls). I went ahead and re-wrote the proc and retested to a very pleasing drop in the cpu usage.

proc
Reverse(var/T as text)
var/newT=""
var/i=-1
while(lentext(T)>lentext(newT))
newT+=copytext(T,i)
i--
return newT
Reverse_List(var/list/L)
var/a=L.len
var/list/b=new()
while(a)
b.Add(L[a])
a--
return b
list2text(var/list/L,var/a as text)
if(!L) return
if(!a) a=","
var/T=""
for(var/l in L)
if(!T) T+="[l]"
else T+="[a][l]"
if(findtextEx(T,a,1,2)) T=("EMPTY"+T)
if(findtextEx(T,a,-1)) T=(T+"[a]EMPTY")
while(findtextEx(T,"[a][a]")) T=replaceText(T,"[a][a]","[a]EMPTY[a]")
return T
text2list(var/T as text,var/a as text)
if(!T) return
if(!a) a=","
var/list/L=new()
if(!findtextEx(T,a)) {L.Add(T); return L}
if(findtextEx(T,a,1,2)) T=("EMPTY"+T)
if(findtextEx(T,a,lentext(T))) T=(T+"EMPTY")
while(findtextEx(T,"[a][a]")) T=replaceText(T,"[a][a]","[a]EMPTY[a]")
while(findtextEx(T,a,1))
var/f=findtextEx(T,a,1)
var/i=copytext(T,1,f)
L.Add(i)
T=copytext(T,f+1)
return L

The above of the proc codes I used for these test.
Below is the test I used.
mob
verb
ListPreformanceTest()
var/list/L=list("What","You","Say","ToMe","kk")
var/i=100000//altered this to 1mil for other test.
while(i)
i--
text2list(Reverse(list2text(Reverse_List(L))))


I included 5 text entries into a list, equaling to 16 total characters.

I would like to have input from other programmers to help me correct anything that'd allow for better preformance without taking away functionality.

I would also like others to attempt their own test and share with me their results, so that I can see how it effects others.
replaceText() isn't defined for me. Not sure which library is that so I cannot test most of your code.

The only thing I tested was:
/proc/Reverse_List 5.349 5.417 5.625 1000000

If I got all indexes right then code should work right away.
proc
Reverse_List(var/list/L)
var/list/b = L.Copy()
for(var/i = 1; i < L.len / 2; i++)
b.Swap(i, L.len - i + 1)
return b
/*
Profile results:
/proc/Reverse_List 3.497 3.583 3.809 1000000
*/

In response to Zaoshi (#1)
I apologize.
proc/replaceText(String, Search, Replace)
if(!findtextEx(String, Search)) return String
var/String_len = lentext(String)
var/Search_len = lentext(Search)
var/Replace_len = lentext(Replace)
for(var/Pos = 1, Pos <= String_len, Pos++)
if(cmptextEx(copytext(String, Pos, Pos+Search_len), Search))
String = copytext(String, 1, Pos) + (Replace) + copytext(String, Pos+Search_len)
String_len += Replace_len - Search_len
return(String)


I forget which libary it's from, but I'm sure the author was Spuzzum. I combined some of his works into a single file.

[EDIT]
I haven't done a whole lot of work with the pre-defined functions for list in BYOND.
[END EDIT]
In response to Zaoshi (#1)
Profile results (total time)
Proc Name Self CPU Total CPU Real Time Calls
----------------------------- --------- --------- --------- ---------
/proc/Reverse_List 4.400 4.496 4.808 1000000

It does present a slight improvement over my own, very nice.

I thank you for your input. I'll not simply take your code however, I will take your correction into account though.
In response to NNAAAAHH (#2)
You can optimize that replaceText() by using findtextEx() to jump to the exact index of each occurrence instead of checking at every index.
In response to DarkCampainger (#4)
I actually attempted to create my own replaceText by using findtextEx(), but it resulted in a higher cpu on mass calls for some reason. I planned on revisiting the subject at a later date, I suppose I could do so now.
In response to NNAAAAHH (#3)
So long as Zaoshi doesn't mind and you understand what's happening, taking the code is not a problem. In fact, my Maptext "Library" originated from me taking a snippet that Kaiochao posted and putting my own touches on it.
In response to Albro1 (#6)
I know, and I'm sure he wouldn't mind if I took the code. I however, do mind. I do not wish to recieve things I should do on my own; I'm using the replies as refrences to improve myself in this feild.