
{"id":4827,"date":"2023-01-03T12:20:52","date_gmt":"2023-01-03T17:20:52","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4827"},"modified":"2023-01-03T12:23:05","modified_gmt":"2023-01-03T17:23:05","slug":"five-letters-problem-python-vs-c-vs-c","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4827","title":{"rendered":"Five letters problem: Python vs C++ vs C#"},"content":{"rendered":"<p>The task is to find groups of 5 English words in which each letter appears at most once, i.e. 25 letters total. These must be &#8220;real&#8221; English words from the dictionary, e.g. <code>fjord gucks nymph vibex waltz<\/code>. I chose an algorithm and wrote an implementation in C++, Python and C#:<br \/>\n<a href=\"https:\/\/github.com\/ikriv-samples\/FiveLetters\">https:\/\/github.com\/ikriv-samples\/FiveLetters<\/a>.<\/p>\n<p>This may be not the most optimal algorithm, but this post about comparison of the languages.<\/p>\n<ul>\n<li>C++ is fast (30s), but verbose. Working with dictionaries is hell.<\/li>\n<li>Python is slow (600s), but easy to write. The lack of types bites you later.<\/li>\n<li>C# is in the middle (100s). OK to write. Strong typing without typedefs is pain. Literals are a major pain.<\/li>\n<\/ul>\n<p>The algorithm I chose may not be the fastest, but the important point it&#8217;s the same in all 3 languages. The program is single threaded. Paralellizing may be the easiest in C# with parallel foreach, but I haven&#8217;t tried it yet.<\/p>\n<p>C++: great performance, but verbose, and working with dictionaries and iterators is painful. Surprisingly, memory management was not a big deal, with vectors and other goodies, it&#8217;s relatively straightforward.<\/p>\n<p>Python: a pleasure to write, but when you come back to the project 4 months later, trying to remember that this thing is a map from integer to a list of integer pais is kind of hard. Yes, Python has type annotations, but then they make it almost as verbose as C# (<code>Dict[int,List[Tuple[int,int]]]<\/code> is no fun), you&#8217;d need to import all this stuff from <code>typing<\/code>, and still the support for type correctness is an afterthought.<\/p>\n<p>C#: lack of proper typedefs is painful. <code>using Type=stuff<\/code> alleviates it somewhat, but it has quirks: see <a href=\"https:\/\/stackoverflow.com\/questions\/74989078\/c-given-type-alias-x-why-i-cant-use-listx-in-a-definition-of-another-type&gt;this Stackoverflow question\">this StackOverflow question<\/a>. Literals for structured data are a big pain. What in Python looks like <code>[{ 0 : [(0,0)] }]<\/code>, and even in C++ loosk liek <code>{ 0,{{0,0}} }<\/code>, translates to C# as<\/p>\n<p><code>new List { new MaskToPairMap { { 0, new List { (0, 0) } } } }<\/code>.<\/p>\n<p>I remember writing special functions for making literals like this shorter and readable.<br \/>\nThe good things going for C# are LINQ and nice lambdas, but I did not have much use for them in this particular case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task is to find groups of 5 English words in which each letter appears at most once, i.e. 25 letters total. These must be &#8220;real&#8221; English words from the <a href=\"https:\/\/ikriv.com\/blog\/?p=4827\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[1],"tags":[],"class_list":["entry","author-ikriv","post-4827","post","type-post","status-publish","format-standard","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4827"}],"version-history":[{"count":4,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4827\/revisions"}],"predecessor-version":[{"id":4831,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4827\/revisions\/4831"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}