Empyrion: Galactic Survival Wiki
Explore
Main Page
All Pages
Interactive Maps
navigation
Main page
Community portal
Recent changes
Random page
Admin noticeboard
Gamepedia
Gamepedia support
Report a bad ad
Help Wiki
Contact us
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Empyrion: Galactic Survival Wiki
976
pages
Explore
Main Page
All Pages
Interactive Maps
navigation
Main page
Community portal
Recent changes
Random page
Admin noticeboard
Gamepedia
Gamepedia support
Report a bad ad
Help Wiki
Contact us
Editing
Game api tuts replace
Back to page
Edit
VisualEditor
View history
Talk (0)
Edit Page
Game api tuts replace
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{GameAPI second tier|Index=GameAPI}} ==Using string replacement== ===Purpose=== You might have quite a few situations, if you are using user-inputs, where you may want to replace something with something else.<br> This could be a test string such as <br> "This is a test string"<br> or "This is a test {keyword(10)} string"<br><br> The purpose of this entry, is to help you improve performance, when using replacements. ===Notes=== Every meassurement is made with Stopwatch, .Elapsed.Ticks<br><br> Each value is the average over using it 10000 times.<br> ===Findings=== IndexOf is normally not recommended to use.<br> If using it, make sure to do it with System.StringComparison.OrdinalIgnoreCase (Unless you need it to be case-sensitive ofc)<br> Contains seems to be a considerably better alternative.<br> <br><br> If you are using a normal replace function: Don't wrap it<br> If you are using a regex.replace function: You should def. wrap it.<br> If you are using a regex.matches: You should absoloutly wrap it, unless you expect a very high match % ===Data=== ====Normal replace==== 10000 iterations over 10000 function calls: -Short string<br> ~5300 - Standard, no matches<br> ~6400 - Contains, no matches<br> ~55600 - IndexOf, no matches<br> ~8560 - IndexOf,ordinalignore, no matches<br> <br> ~15000 - Standard, matches<br> ~23000 - Contains, matches<br> ~73800 - IndexOf, matches<br> ~28000 - IndexOf,ordinalignore, matches<br> <br> -Long string<br> ~39530 - Standard, no matches<br> ~32840 - Contains, no matches<br> ~616400 - IndexOf, no matches<br> ~86820 - IndexOf,ordinalignore, no matches<br><br> ~45920 - Standard, early matches<br> ~54200 - Contains, early matches<br> ~151770 - IndexOf, early matches -- Yes, this is quite strange, but I have checked it 3 times.<br> ~64900 - IndexOf,ordinalignore, early matches<br><br> ~42730 - Standard, late matches<br> ~74000 - Contains, late matches<br> ~652130 - IndexOf, late matches<br> ~124470 - IndexOf,ordinalignore, late matches<br> <br> ---comment:<br> Suggested approach: Don't wrap a normal .Replace ====Regex replace==== 1000 iterations over 10000 function calls: -Short string<br> ~551600 - Standard, no matches<br> ~7122 - Contains, no matches<br> ~56900 - IndexOf, no matches<br> ~9531 - IndexOf,ordinalignore, no matches<br> <br> ~625000 - Standard, matches<br> ~664037 - Contains, matches<br> ~711400 - IndexOf, matches<br> ~669900 - IndexOf,ordinalignore, matches<br><br> -Long string<br> ~622300 - Standard, no matches<br> ~34330 - Contains, no matches<br> ~614000 - IndexOf, no matches<br> ~83330 - IndexOf,ordinalignore, no matches<br><br> ~595400 - Standard, early matches<br> ~651100 - Contains, early matches<br> ~788200 - IndexOf, early matches<br> ~644650 - IndexOf,ordinalignore, early matches<br><br> ~599700 - Standard, late matches<br> ~690000 - Contains, late matches<br> ~1262500 - IndexOf, late matches<br> ~755900 - IndexOf,ordinalignore, late matches<br> <br> ---comment:<br> Wrapping a regex replace in anything improves the performance by quite a bit. Ideally, use contains.<br> The loss of performance in the case, where there's matches is small, compared to the gain, where there's not.<br> However, unless you need to use regex, you really should use normal .replace<br> <br> ====Regex .matches==== going into a .Replace for each match, 1000 iterations over 10000 function calls: -Short string<br> ~568200 - Standard, no matches<br> ~8300 - Contains, no matches<br> ~57150 - IndexOf, no matches<br> ~9780 - IndexOf,ordinalignore, no matches<br> <br> ~708300 - Standard, matches<br> ~711700 - Contains, matches<br> ~766600 - IndexOf, matches<br> ~731000 - IndexOf,ordinalignore, matches<br><br> -Long string<br> ~629800 - Standard, no matches<br> ~34430 - Contains, no matches<br> ~627750 - IndexOf, no matches<br> ~86200 - IndexOf,ordinalignore, no matches<br><br> ~828500 - Standard, early matches<br> ~901700 - Contains, early matches<br> ~1045900 - IndexOf, early matches<br> ~887800 - IndexOf,ordinalignore, early matches<br><br> ~813700 - Standard, late matches<br> ~963000 - Contains, late matches<br> ~1575200 - IndexOf, late matches<br> ~1032150 - IndexOf,ordinalignore, late matches<br> <br> ---comment:<br> The gain from wrapping is pretty sizeable. Ideally should be wrapped in contains.<br> The loss is fairly insignificent, on matches. When dealing with long strings, you may want to consider how frequent you are expecting results. If it's a very high %, it's possibly not worth to wrap it. === Code used to perform tests === Uncomment one entry in each section using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Text.RegularExpressions; namespace performancetests { class Program { static void Main(string[] args) { long avg = 0; int iterations = 1000; Console.WriteLine("Average test"); for (var ii = 0; ii < iterations; ii++) { Stopwatch sw = new Stopwatch(); sw.Start(); //string input = "This is a test string." //string input = "This is a {teststring(10)} string." //Long strings //string input = "This is a test string. Such a long teststring this is. It is very impressive. I'm so amazed that this is a teststring, I mean really tho? How can something be this neat? Let's just keep testing! Pewpewpew. It's a good time. Who cares if a teststring is weird."; string input = "This is a test string. Such a long {teststring(10)} teststring this is. It is very impressive. I'm so amazed that this is a teststring, I mean really tho? How can something be this neat? Let's just keep testing! Pewpewpew. It's a good time. Who cares if a teststring is weird."; //string input = "This is a test string. Such a long teststring this is. It is very impressive. I'm so amazed that this is a teststring, I mean really tho? How can something be this neat? Let's just keep testing! Pewpewpew. It's a good time. Who cares if a teststring {teststring(10)} is weird."; string input2 = ""; for (var i = 0; i < 10000; i++) { input2 = input; if (input2.IndexOf("{teststring", System.StringComparison.OrdinalIgnoreCase) != -1) //if(input2.Contains("{teststring")) //if(true) //if (input2.IndexOf("{teststring") != -1) { //input2 = input2.Replace("{teststring}", "yup"); Regex rgx = new Regex(@"\{test[s]tring\}"); input2 = rgx.Replace(input2, "yup"); /*string patterncda = @"\{teststring\([0-9]*\)\}"; Regex rgxcda = new Regex(patterncda); foreach (Match match in rgxcda.Matches(input)) { input2 = input2.Replace(match.Value, "yup"); } */ } } sw.Stop(); avg += sw.Elapsed.Ticks; Console.WriteLine("{1} : Elapsed={0}", sw.Elapsed,ii); } avg = avg / iterations; Console.WriteLine("Press ESC to stop. Average was {0}",avg); do { while (!Console.KeyAvailable) { } } while (Console.ReadKey(true).Key != ConsoleKey.Escape); } } }
Summary:
Please note that all contributions to the Empyrion: Galactic Survival Wiki are considered to be released under the CC BY-SA 3.0
Cancel
Editing help
(opens in new window)
Template used on this page:
Template:GameAPI second tier
(
view source
)
Follow on IG
TikTok
Join Fan Lab