{"id":27666,"date":"2022-12-25T17:59:37","date_gmt":"2022-12-25T12:29:37","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/"},"modified":"2022-12-25T17:59:37","modified_gmt":"2022-12-25T12:29:37","slug":"solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/","title":{"rendered":"[Solved] Fastest way to get a part of a string (int) with a delimiter in C#"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-43353341\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"43353341\" data-parentid=\"43352570\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Just for fun and surprisingly fast:<\/p>\n<pre><code>public static int GetSignature(string value)\n{\n  int result = 0;\n  for (int i = 1; i &lt; 12; i++) \/\/ 12 should be more than enough for Int32\n  {\n    if (value[i] == '&gt;')\n    {\n      break;\n    }\n    result = (result * 10) + (value[i] - '0');\n  }\n  return result;\n}\n<\/code><\/pre>\n<hr>\n<p>Edit: My quick&#8217;n&#8217;dirty test setup<\/p>\n<pre><code>class Program\n{\n  static void Main(string[] args)\n  {\n    Run(new Random());\n\n    Console.ReadLine();\n  }\n\n  private static void Run(Random rnd)\n  {\n    var counts = new[] { 10, 100000, 10000000 };\n\n    foreach (var count in counts)\n    {\n      Console.WriteLine(count);\n      Run(count, rnd);\n      Console.WriteLine();\n    }\n  }\n\n  private static void Run(int count, Random rnd)\n  {\n    var values = GetValues(count, rnd);\n\n    var funcs\n      = new Dictionary&lt;string, Func&lt;string, int&gt;&gt;\n      {\n        {\"OP\", GetSignatureOP},\n        {\"Keith\", GetSignatureKeith},\n        {\"Fun\", GetSignatureFun},\n      };\n\n    foreach (var kvp in funcs)\n    {\n      TimeSpan elapsed;\n      Test(values, kvp.Value, out elapsed);\n      Console.WriteLine(\"{0,-5}: {1:G}\", kvp.Key, elapsed);\n    }\n  }\n\n  private static IList&lt;string&gt; GetValues(int count, Random rnd)\n  {\n    var result = new List&lt;string&gt;(count);\n\n    for (int index = 0; index &lt; count; index++)\n    {\n      result.Add(string.Format(\"&lt;{0}&gt;something here just not relevant&lt;\/{0}&gt;\", rnd.Next(1, 10)));\n    }\n\n    return result;\n  }\n\n  private static int Test(IEnumerable&lt;string&gt; values, Func&lt;string, int&gt; func, out TimeSpan elapsed)\n  {\n    GC.Collect();\n    GC.WaitForPendingFinalizers();\n\n    var sw = Stopwatch.StartNew();\n    var count = values.Aggregate(0, (current, value) =&gt; current ^ func(value));\n    sw.Stop();\n\n    elapsed = sw.Elapsed;\n\n    return count;\n  }\n\n\n  private static int GetSignatureOP(string value)\n  {\n    return Convert.ToInt32(value.Split('&gt;')[0].Remove(0, 1));\n  }\n  private static int GetSignatureKeith(string value)\n  {\n    return int.Parse(value.Substring(1, value.IndexOf('&gt;') - 1));\n  }\n  private static int GetSignatureFun(string value)\n  {\n    int result = 0;\n    for (int i = 1; i &lt; 12; i++)\n    {\n      if (value[i] == '&gt;')\n      {\n        break;\n      }\n      result = (result * 10) + (value[i] - '0');\n    }\n    return result;\n  }\n}\n<\/code><\/pre>\n<p>Results (on my machine):<\/p>\n<pre><code>10\nOP   : 0:00:00:00,0007532\nKeith: 0:00:00:00,0001523\nFun  : 0:00:00:00,0001307\n\n100000\nOP   : 0:00:00:00,0306495\nKeith: 0:00:00:00,0116116\nFun  : 0:00:00:00,0018416\n\n10000000\nOP   : 0:00:00:02,7450986\nKeith: 0:00:00:01,1598363\nFun  : 0:00:00:00,1855654\n<\/code><\/pre>\n<p>And with random values for <code>rnd.Next(1, int.MaxValue)<\/code>:<\/p>\n<pre><code>10\nOP   : 0:00:00:00,0006975\nKeith: 0:00:00:00,0001147\nFun  : 0:00:00:00,0001246\n\n100000\nOP   : 0:00:00:00,0409755\nKeith: 0:00:00:00,0187789\nFun  : 0:00:00:00,0030894\n\n10000000\nOP   : 0:00:00:04,0060685\nKeith: 0:00:00:01,9214684\nFun  : 0:00:00:00,3083399\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Fastest way to get a part of a string (int) with a delimiter in C# <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Just for fun and surprisingly fast: public static int GetSignature(string value) { int result = 0; for (int i = 1; i &lt; 12; i++) \/\/ 12 should be more than enough for Int32 { if (value[i] == &#8216;&gt;&#8217;) { break; } result = (result * 10) + (value[i] &#8211; &#8216;0&#8217;); } return result; &#8230; <a title=\"[Solved] Fastest way to get a part of a string (int) with a delimiter in C#\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/\" aria-label=\"More on [Solved] Fastest way to get a part of a string (int) with a delimiter in C#\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-27666","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Fastest way to get a part of a string (int) with a delimiter in C# - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Fastest way to get a part of a string (int) with a delimiter in C# - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Just for fun and surprisingly fast: public static int GetSignature(string value) { int result = 0; for (int i = 1; i &lt; 12; i++) \/\/ 12 should be more than enough for Int32 { if (value[i] == &#039;&gt;&#039;) { break; } result = (result * 10) + (value[i] - &#039;0&#039;); } return result; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-25T12:29:37+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Fastest way to get a part of a string (int) with a delimiter in C#\",\"datePublished\":\"2022-12-25T12:29:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/\"},\"wordCount\":56,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/\",\"name\":\"[Solved] Fastest way to get a part of a string (int) with a delimiter in C# - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-25T12:29:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Fastest way to get a part of a string (int) with a delimiter in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Fastest way to get a part of a string (int) with a delimiter in C# - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Fastest way to get a part of a string (int) with a delimiter in C# - JassWeb","og_description":"[ad_1] Just for fun and surprisingly fast: public static int GetSignature(string value) { int result = 0; for (int i = 1; i &lt; 12; i++) \/\/ 12 should be more than enough for Int32 { if (value[i] == '&gt;') { break; } result = (result * 10) + (value[i] - '0'); } return result; ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/","og_site_name":"JassWeb","article_published_time":"2022-12-25T12:29:37+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Fastest way to get a part of a string (int) with a delimiter in C#","datePublished":"2022-12-25T12:29:37+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/"},"wordCount":56,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/","url":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/","name":"[Solved] Fastest way to get a part of a string (int) with a delimiter in C# - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-25T12:29:37+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-fastest-way-to-get-a-part-of-a-string-int-with-a-delimiter-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Fastest way to get a part of a string (int) with a delimiter in C#"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=27666"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27666\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=27666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=27666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=27666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}