![]() Strings are hashed, so this is not a better way.īecause the loop is slower, it also has lower limits. What about concatenating strings? No, that may be slightly faster, but worse for performance and probably takes up much more memory. I also tried storing all letters in a dictionary, although that turned out to be slightly slower. Letters = v - probably faster than table.insert() If (bc >= 65 and bc = 97 and bc <= 122) then Here is the loop code: local n, bc = 0, nilįor i, v in pairs(string.split(str, "")) do Again, from six results I removed the two most deviant ones. This time the test was done on 10 * 10^6 characters long string. Keep in mind that this is of rather informative nature. I ran the test again using a faster version of the loop, but I found no way to write a quicker function inside native Roblox studio environment. Thank you too for reminding me of string.match(str, “%a”, ‘’)!Īs far as letter isolation goes, gsub is a standard built-in function, running as privileged code and is part of string library written as a C module. Local firstWord = string.match(phrase, "^%A*(%a+)")įirstWord = string.gsub(firstWord, "%A", "") - keep only alphabetic thank you for correcting me of that! Yes, I did mean alphabetic, and I also corrected string.match(phrase, “^%A*(%a+)”) as you proposed. Local firstWord = string.split(phrase, " ") Split the phrase into a table, and separate elements The former option seems easier to read and write.įinal version local phrase = "Hello, world" A small disadvantage of loop method is also that it doesn’t respond well to special characters, namely open brackets “(” and close brackets “)” in my case.Īs far as the first word isolation goes: string.match(phrase, "^(.+)%s") gsub method is still recommended, because it is obviously simple, faster, more practical and requires less code. After I got all the results, I removed the two most deviant ones and calculated the average:Īs you can see from the results, gsub method is faster, but the difference is almost negligible, at least when it comes to relatively short strings. The same procedure was repeated 6 times for each method. The string I chose consisted of 50 randomly generated characters: lower and upper case, numbers and all common puncuation marks. ![]() In my quick test I ran both examples and measured their process time. I was interested in quick comparison of both methods (now my previous post is corrected and doesn’t include it anymore, because as expected, it is fast, but not the most efficient). Unfortunately, at the time of writing above post, I forgot about that option existing. ![]() You are right, string.gsub(firstWord, "%A", '') is indeed the most efficient way of isolating ASCII characters.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |